|
@@ -33,11 +33,29 @@ class PeerOptions implements PeerJSOption {
|
|
}
|
|
}
|
|
|
|
|
|
type PeerEvents = {
|
|
type PeerEvents = {
|
|
|
|
+ /**
|
|
|
|
+ * Emitted when a connection to the PeerServer is established.
|
|
|
|
+ */
|
|
open: (id: string) => void;
|
|
open: (id: string) => void;
|
|
|
|
+ /**
|
|
|
|
+ * Emitted when a new data connection is established from a remote peer.
|
|
|
|
+ */
|
|
connection: (dataConnection: DataConnection) => void;
|
|
connection: (dataConnection: DataConnection) => void;
|
|
|
|
+ /**
|
|
|
|
+ * Emitted when a remote peer attempts to call you.
|
|
|
|
+ */
|
|
call: (mediaConnection: MediaConnection) => void;
|
|
call: (mediaConnection: MediaConnection) => void;
|
|
|
|
+ /**
|
|
|
|
+ * Emitted when the peer is destroyed and can no longer accept or create any new connections.
|
|
|
|
+ */
|
|
close: () => void;
|
|
close: () => void;
|
|
|
|
+ /**
|
|
|
|
+ * Emitted when the peer is disconnected from the signalling server
|
|
|
|
+ */
|
|
disconnected: (currentId: string) => void;
|
|
disconnected: (currentId: string) => void;
|
|
|
|
+ /**
|
|
|
|
+ * Errors on the peer are almost always fatal and will destroy the peer.
|
|
|
|
+ */
|
|
error: (error: Error) => void;
|
|
error: (error: Error) => void;
|
|
};
|
|
};
|
|
/**
|
|
/**
|
|
@@ -59,7 +77,9 @@ export class Peer extends EventEmitter<PeerEvents> {
|
|
private _open = false; // Sockets and such are not yet open.
|
|
private _open = false; // Sockets and such are not yet open.
|
|
private readonly _connections: Map<string, BaseConnection[]> = new Map(); // All connections for this peer.
|
|
private readonly _connections: Map<string, BaseConnection[]> = new Map(); // All connections for this peer.
|
|
private readonly _lostMessages: Map<string, ServerMessage[]> = new Map(); // src => [list of messages]
|
|
private readonly _lostMessages: Map<string, ServerMessage[]> = new Map(); // src => [list of messages]
|
|
-
|
|
|
|
|
|
+ /**
|
|
|
|
+ * The brokering ID of this peer
|
|
|
|
+ */
|
|
get id() {
|
|
get id() {
|
|
return this._id;
|
|
return this._id;
|
|
}
|
|
}
|
|
@@ -77,6 +97,7 @@ export class Peer extends EventEmitter<PeerEvents> {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * A hash of all connections associated with this peer, keyed by the remote peer's ID.
|
|
* @deprecated
|
|
* @deprecated
|
|
* Return type will change from Object to Map<string,[]>
|
|
* Return type will change from Object to Map<string,[]>
|
|
*/
|
|
*/
|
|
@@ -90,13 +111,38 @@ export class Peer extends EventEmitter<PeerEvents> {
|
|
return plainConnections;
|
|
return plainConnections;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * true if this peer and all of its connections can no longer be used.
|
|
|
|
+ */
|
|
get destroyed() {
|
|
get destroyed() {
|
|
return this._destroyed;
|
|
return this._destroyed;
|
|
}
|
|
}
|
|
|
|
+ /**
|
|
|
|
+ * false if there is an active connection to the PeerServer.
|
|
|
|
+ */
|
|
get disconnected() {
|
|
get disconnected() {
|
|
return this._disconnected;
|
|
return this._disconnected;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * A peer can connect to other peers and listen for connections.
|
|
|
|
+ */
|
|
|
|
+ constructor();
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * A peer can connect to other peers and listen for connections.
|
|
|
|
+ * @param options for specifying details about PeerServer
|
|
|
|
+ */
|
|
|
|
+ constructor(options: PeerOptions);
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * A peer can connect to other peers and listen for connections.
|
|
|
|
+ * @param id Other peers can connect to this peer using the provided ID.
|
|
|
|
+ * If no ID is given, one will be generated by the brokering server.
|
|
|
|
+ * @param options for specifying details about PeerServer
|
|
|
|
+ */
|
|
|
|
+ constructor(id: string, options?: PeerOptions);
|
|
|
|
+
|
|
constructor(id?: string | PeerOptions, options?: PeerOptions) {
|
|
constructor(id?: string | PeerOptions, options?: PeerOptions) {
|
|
super();
|
|
super();
|
|
|
|
|
|
@@ -359,8 +405,9 @@ export class Peer extends EventEmitter<PeerEvents> {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Returns a DataConnection to the specified peer. See documentation for a
|
|
|
|
- * complete list of options.
|
|
|
|
|
|
+ * Connects to the remote peer specified by id and returns a data connection.
|
|
|
|
+ * @param peer The brokering ID of the remote peer (their peer.id).
|
|
|
|
+ * @param options for specifying details about Peer Connection
|
|
*/
|
|
*/
|
|
connect(peer: string, options: PeerConnectOption = {}): DataConnection {
|
|
connect(peer: string, options: PeerConnectOption = {}): DataConnection {
|
|
if (this.disconnected) {
|
|
if (this.disconnected) {
|
|
@@ -383,8 +430,10 @@ export class Peer extends EventEmitter<PeerEvents> {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * Returns a MediaConnection to the specified peer. See documentation for a
|
|
|
|
- * complete list of options.
|
|
|
|
|
|
+ * Calls the remote peer specified by id and returns a media connection.
|
|
|
|
+ * @param peer The brokering ID of the remote peer (their peer.id).
|
|
|
|
+ * @param stream The caller's media stream
|
|
|
|
+ * @param options Metadata associated with the connection, passed in by whoever initiated the connection.
|
|
*/
|
|
*/
|
|
call(
|
|
call(
|
|
peer: string,
|
|
peer: string,
|