UNPKG

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