index.d.ts 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. // Type definitions for the PeerJS class module
  2. // Original definitions by Toshiya Nakakura <https://github.com/nakakura>
  3. // at https://github.com/DefinitelyTyped/DefinitelyTyped
  4. export = Peer;
  5. declare class Peer {
  6. prototype: RTCIceServer;
  7. /**
  8. * A peer can connect to other peers and listen for connections.
  9. * @param id Other peers can connect to this peer using the provided ID.
  10. * If no ID is given, one will be generated by the brokering server.
  11. * @param options for specifying details about PeerServer
  12. */
  13. constructor(id: string, options?: Peer.PeerJSOption);
  14. /**
  15. * A peer can connect to other peers and listen for connections.
  16. * @param options for specifying details about PeerServer
  17. */
  18. constructor(options: Peer.PeerJSOption);
  19. /**
  20. *
  21. * @param id The brokering ID of the remote peer (their peer.id).
  22. * @param options for specifying details about Peer Connection
  23. */
  24. connect(id: string, options?: Peer.PeerConnectOption): Peer.DataConnection;
  25. /**
  26. * Connects to the remote peer specified by id and returns a data connection.
  27. * @param id The brokering ID of the remote peer (their peer.id).
  28. * @param stream The caller's media stream
  29. * @param options Metadata associated with the connection, passed in by whoever initiated the connection.
  30. */
  31. call(id: string, stream: any, options?: any): Peer.MediaConnection;
  32. /**
  33. * Calls the remote peer specified by id and returns a media connection.
  34. * @param event Event name
  35. * @param cb Callback Function
  36. */
  37. on(event: string, cb: ()=>void): void;
  38. /**
  39. * Emitted when a connection to the PeerServer is established.
  40. * @param event Event name
  41. * @param cb id is the brokering ID of the peer
  42. */
  43. on(event: 'open', cb: (id: string)=>void): void;
  44. /**
  45. * Emitted when a new data connection is established from a remote peer.
  46. * @param event Event name
  47. * @param cb Callback Function
  48. */
  49. on(event: 'connection', cb: (dataConnection: Peer.DataConnection)=>void): void;
  50. /**
  51. * Emitted when a remote peer attempts to call you.
  52. * @param event Event name
  53. * @param cb Callback Function
  54. */
  55. on(event: 'call', cb: (mediaConnection: Peer.MediaConnection)=>void): void;
  56. /**
  57. * Emitted when the peer is destroyed and can no longer accept or create any new connections.
  58. * @param event Event name
  59. * @param cb Callback Function
  60. */
  61. on(event: 'close', cb: ()=>void): void;
  62. /**
  63. * Emitted when the peer is disconnected from the signalling server
  64. * @param event Event name
  65. * @param cb Callback Function
  66. */
  67. on(event: 'disconnected', cb: ()=>void): void;
  68. /**
  69. * Errors on the peer are almost always fatal and will destroy the peer.
  70. * @param event Event name
  71. * @param cb Callback Function
  72. */
  73. on(event: 'error', cb: (err: any)=>void): void;
  74. /**
  75. * Remove event listeners.(EventEmitter3)
  76. * @param {String} event The event we want to remove.
  77. * @param {Function} fn The listener that we need to find.
  78. * @param {Boolean} once Only remove once listeners.
  79. */
  80. off(event: string, fn: Function, once?: boolean): void;
  81. /**
  82. * Close the connection to the server, leaving all existing data and media connections intact.
  83. */
  84. disconnect(): void;
  85. /**
  86. * Attempt to reconnect to the server with the peer's old ID
  87. */
  88. reconnect(): void;
  89. /**
  90. * Close the connection to the server and terminate all existing connections.
  91. */
  92. destroy(): void;
  93. /**
  94. * Retrieve a data/media connection for this peer.
  95. * @param peer
  96. * @param id
  97. */
  98. getConnection(peer: Peer, id: string): any;
  99. /**
  100. * Get a list of available peer IDs
  101. * @param callback
  102. */
  103. listAllPeers(callback: (peerIds: Array<string>)=>void): void;
  104. /**
  105. * The brokering ID of this peer
  106. */
  107. id: string;
  108. /**
  109. * A hash of all connections associated with this peer, keyed by the remote peer's ID.
  110. */
  111. connections: any;
  112. /**
  113. * false if there is an active connection to the PeerServer.
  114. */
  115. disconnected: boolean;
  116. /**
  117. * true if this peer and all of its connections can no longer be used.
  118. */
  119. destroyed: boolean;
  120. }
  121. declare namespace Peer {
  122. interface PeerJSOption{
  123. key?: string;
  124. host?: string;
  125. port?: number;
  126. path?: string;
  127. secure?: boolean;
  128. config?: RTCConfiguration;
  129. debug?: number;
  130. }
  131. interface PeerConnectOption{
  132. label?: string;
  133. metadata?: any;
  134. serialization?: string;
  135. reliable?: boolean;
  136. }
  137. interface DataConnection{
  138. send(data: any): void;
  139. close(): void;
  140. on(event: string, cb: ()=>void): void;
  141. on(event: 'data', cb: (data: any)=>void): void;
  142. on(event: 'open', cb: ()=>void): void;
  143. on(event: 'close', cb: ()=>void): void;
  144. on(event: 'error', cb: (err: any)=>void): void;
  145. off(event: string, fn: Function, once?: boolean): void;
  146. dataChannel: RTCDataChannel;
  147. label: string;
  148. metadata: any;
  149. open: boolean;
  150. peerConnection: any;
  151. peer: string;
  152. reliable: boolean;
  153. serialization: string;
  154. type: string;
  155. buffSize: number;
  156. }
  157. interface MediaConnection{
  158. answer(stream?: any): void;
  159. close(): void;
  160. on(event: string, cb: ()=>void): void;
  161. on(event: 'stream', cb: (stream: any)=>void): void;
  162. on(event: 'close', cb: ()=>void): void;
  163. on(event: 'error', cb: (err: any)=>void): void;
  164. off(event: string, fn: Function, once?: boolean): void;
  165. open: boolean;
  166. metadata: any;
  167. peer: string;
  168. type: string;
  169. }
  170. interface utilSupportsObj {
  171. audioVideo: boolean;
  172. data: boolean;
  173. binary: boolean;
  174. reliable: boolean;
  175. }
  176. interface util{
  177. browser: string;
  178. supports: utilSupportsObj;
  179. }
  180. }