index.d.ts 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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?: 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(
  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 peer
  99. * @param id
  100. */
  101. getConnection(peer: Peer, id: string): any;
  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 DataConnection {
  141. send(data: any): void;
  142. close(): void;
  143. on(event: string, cb: () => void): void;
  144. on(event: "data", cb: (data: any) => void): void;
  145. on(event: "open", cb: () => void): void;
  146. on(event: "close", cb: () => void): void;
  147. on(event: "error", cb: (err: any) => void): void;
  148. off(event: string, fn: Function, once?: boolean): void;
  149. dataChannel: RTCDataChannel;
  150. label: string;
  151. metadata: any;
  152. open: boolean;
  153. peerConnection: any;
  154. peer: string;
  155. reliable: boolean;
  156. serialization: string;
  157. type: string;
  158. buffSize: number;
  159. }
  160. interface MediaConnection {
  161. answer(stream?: any): void;
  162. close(): void;
  163. on(event: string, cb: () => void): void;
  164. on(event: "stream", cb: (stream: any) => void): void;
  165. on(event: "close", cb: () => void): void;
  166. on(event: "error", cb: (err: any) => void): void;
  167. off(event: string, fn: Function, once?: boolean): void;
  168. open: boolean;
  169. metadata: any;
  170. peer: string;
  171. type: string;
  172. }
  173. interface utilSupportsObj {
  174. audioVideo: boolean;
  175. data: boolean;
  176. binary: boolean;
  177. reliable: boolean;
  178. }
  179. interface util {
  180. browser: string;
  181. supports: utilSupportsObj;
  182. }
  183. }