index.d.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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: MediaStream, options?: Peer.CallOption): 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(
  50. event: "connection",
  51. cb: (dataConnection: Peer.DataConnection) => void
  52. ): void;
  53. /**
  54. * Emitted when a remote peer attempts to call you.
  55. * @param event Event name
  56. * @param cb Callback Function
  57. */
  58. on(event: "call", cb: (mediaConnection: Peer.MediaConnection) => void): void;
  59. /**
  60. * Emitted when the peer is destroyed and can no longer accept or create any new connections.
  61. * @param event Event name
  62. * @param cb Callback Function
  63. */
  64. on(event: "close", cb: () => void): void;
  65. /**
  66. * Emitted when the peer is disconnected from the signalling server
  67. * @param event Event name
  68. * @param cb Callback Function
  69. */
  70. on(event: "disconnected", cb: () => void): void;
  71. /**
  72. * Errors on the peer are almost always fatal and will destroy the peer.
  73. * @param event Event name
  74. * @param cb Callback Function
  75. */
  76. on(event: "error", cb: (err: any) => void): void;
  77. /**
  78. * Remove event listeners.(EventEmitter3)
  79. * @param {String} event The event we want to remove.
  80. * @param {Function} fn The listener that we need to find.
  81. * @param {Boolean} once Only remove once listeners.
  82. */
  83. off(event: string, fn: Function, once?: boolean): void;
  84. /**
  85. * Close the connection to the server, leaving all existing data and media connections intact.
  86. */
  87. disconnect(): void;
  88. /**
  89. * Attempt to reconnect to the server with the peer's old ID
  90. */
  91. reconnect(): void;
  92. /**
  93. * Close the connection to the server and terminate all existing connections.
  94. */
  95. destroy(): void;
  96. /**
  97. * Retrieve a data/media connection for this peer.
  98. * @param peerId
  99. * @param connectionId
  100. */
  101. getConnection(peerId: string, connectionId: string): Peer.MediaConnection | Peer.DataConnection | null;
  102. /**
  103. * Get a list of available peer IDs
  104. * @param callback
  105. */
  106. listAllPeers(callback: (peerIds: Array<string>) => void): void;
  107. /**
  108. * The brokering ID of this peer
  109. */
  110. id: string;
  111. /**
  112. * A hash of all connections associated with this peer, keyed by the remote peer's ID.
  113. */
  114. connections: any;
  115. /**
  116. * false if there is an active connection to the PeerServer.
  117. */
  118. disconnected: boolean;
  119. /**
  120. * true if this peer and all of its connections can no longer be used.
  121. */
  122. destroyed: boolean;
  123. }
  124. declare namespace Peer {
  125. interface PeerJSOption {
  126. key?: string;
  127. host?: string;
  128. port?: number;
  129. path?: string;
  130. secure?: boolean;
  131. config?: RTCConfiguration;
  132. debug?: number;
  133. }
  134. interface PeerConnectOption {
  135. label?: string;
  136. metadata?: any;
  137. serialization?: string;
  138. reliable?: boolean;
  139. }
  140. interface CallOption {
  141. metadata?: any;
  142. sdpTransform?: Function;
  143. }
  144. interface AnswerOption {
  145. sdpTransform?: Function;
  146. }
  147. interface DataConnection {
  148. send(data: any): void;
  149. close(): void;
  150. on(event: string, cb: () => void): void;
  151. on(event: "data", cb: (data: any) => void): void;
  152. on(event: "open", cb: () => void): void;
  153. on(event: "close", cb: () => void): void;
  154. on(event: "error", cb: (err: any) => void): void;
  155. off(event: string, fn: Function, once?: boolean): void;
  156. dataChannel: RTCDataChannel;
  157. label: string;
  158. metadata: any;
  159. open: boolean;
  160. peerConnection: RTCPeerConnection;
  161. peer: string;
  162. reliable: boolean;
  163. serialization: string;
  164. type: string;
  165. bufferSize: number;
  166. }
  167. interface MediaConnection {
  168. answer(stream?: MediaStream, options?: AnswerOption): void;
  169. close(): void;
  170. on(event: string, cb: () => void): void;
  171. on(event: "stream", cb: (stream: MediaStream) => void): void;
  172. on(event: "close", cb: () => void): void;
  173. on(event: "error", cb: (err: any) => void): void;
  174. off(event: string, fn: Function, once?: boolean): void;
  175. open: boolean;
  176. metadata: any;
  177. peerConnection: RTCPeerConnection;
  178. peer: string;
  179. type: string;
  180. }
  181. interface utilSupportsObj {
  182. audioVideo: boolean;
  183. data: boolean;
  184. binary: boolean;
  185. reliable: boolean;
  186. }
  187. interface util {
  188. browser: string;
  189. supports: utilSupportsObj;
  190. }
  191. }