From 363faada46532c316f81e2cab85bfe24a60e18bd Mon Sep 17 00:00:00 2001 From: alonbru Date: Sun, 29 Jan 2023 16:34:02 +0200 Subject: [PATCH 1/3] Added _emitError method in DataConnection class Used to emit typed errors --- lib/dataconnection.ts | 31 +++++++++++++++++++++++++------ lib/enums.ts | 3 +++ 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/lib/dataconnection.ts b/lib/dataconnection.ts index a72dfa445..070d61411 100644 --- a/lib/dataconnection.ts +++ b/lib/dataconnection.ts @@ -1,7 +1,12 @@ import { util } from "./util"; import logger from "./logger"; import { Negotiator } from "./negotiator"; -import { ConnectionType, SerializationType, ServerMessageType } from "./enums"; +import { + ConnectionType, + DataConnectionErrorType, + SerializationType, + ServerMessageType, +} from "./enums"; import { Peer } from "./peer"; import { BaseConnection } from "./baseconnection"; import { ServerMessage } from "./servermessage"; @@ -190,7 +195,23 @@ export class DataConnection this._handleDataMessage({ data }); } } + + /** Emits a typed error message. */ + private _emitError(type: DataConnectionErrorType, err: string | Error): void { + logger.error("Error:", err); + let error: Error & { type?: DataConnectionErrorType }; + + if (typeof err === "string") { + error = new Error(err); + } else { + error = err as Error; + } + + error.type = type; + + super.emit("error", error); + } /** * Exposed functionality for users. */ @@ -237,11 +258,9 @@ export class DataConnection /** Allows user to send data. */ send(data: any, chunked?: boolean): void { if (!this.open) { - super.emit( - "error", - new Error( - "Connection is not open. You should listen for the `open` event before sending messages.", - ), + this._emitError( + DataConnectionErrorType.NotOpen, + "Connection is not open. You should listen for the `open` event before sending messages.", ); return; } diff --git a/lib/enums.ts b/lib/enums.ts index a309b9b0b..add0f7ebd 100644 --- a/lib/enums.ts +++ b/lib/enums.ts @@ -17,6 +17,9 @@ export enum PeerErrorType { UnavailableID = "unavailable-id", WebRTC = "webrtc", } +export enum DataConnectionErrorType { + NotOpen = "not-open", +} export enum SerializationType { Binary = "binary", From cc2b8837f69d40676ab6495215bbfec2a1485fe6 Mon Sep 17 00:00:00 2001 From: alonbru Date: Sun, 29 Jan 2023 17:11:51 +0200 Subject: [PATCH 2/3] Added docs for dataConnection error handling --- docs/api.json | 15 ++ docs/index.html | 547 +----------------------------------------------- 2 files changed, 18 insertions(+), 544 deletions(-) diff --git a/docs/api.json b/docs/api.json index 00734688e..e66390d4d 100644 --- a/docs/api.json +++ b/docs/api.json @@ -419,6 +419,21 @@ "name": ".bufferSize", "type": "number", "description": "The number of messages queued to be sent once the browser buffer is no longer full." + }, + { + "name": "'error'", + "type": "event", + "snippet": "dataConnection.on('error', function(err) { ... });", + "description": "", + "children": [ + { + "name": "'not-open'", + "type": "Error", + "tags": [ + ], + "description": "An a attempt to use the DataConnection to send a message was made before the connection was open and ready. Listen for the 'open' event to know when a conncetion is ready." + } + ] } ] }, diff --git a/docs/index.html b/docs/index.html index 5fd3f2508..a78a1c780 100644 --- a/docs/index.html +++ b/docs/index.html @@ -149,8 +149,7 @@

Are there any caveats?

NAT traversal is impossible and no connection can be made. A workaround is to proxy through the connection through a TURN - server. The PeerServer cloud service provides a free TURN server. This will allow your PeerJS app to work - seamlessly for this situation

+ server. The PeerServer cloud service provides a free TURN server. This will allow your PeerJS app to work seamlessly for this situation

How do I use a TURN server?

When creating your Peer object, pass in the ICE servers as the config key of the options hash.

@@ -207,547 +206,7 @@ 

Getting Started

-
Peerconstructorconst peer = new Peer([id], - [options]); -

A peer can connect to other peers and listen for connections.

-
-
[id]string -

Other peers can connect to this peer using the provided ID. If no ID is given, one will - be generated by the brokering server. The ID must start and end with an alphanumeric character (lower or - upper case character or a digit). In the middle of the ID spaces, dashes (-) and underscores (_) are - allowed.It's not recommended that you use this ID to identify peers, as it's meant to be - used for brokering connections only. You're recommended to set the metadata option to send other identifying - information.

-
-
[options]object -
-
keystring -

API key for the cloud PeerServer. This is not used for servers other than - 0.peerjs.com.PeerServer cloud runs on port 443. Please ensure it is not - blocked or consider running your own PeerServer instead.

-
-
hoststring -

Server host. Defaults to 0.peerjs.com. Also accepts '/' - to signify relative hostname.

-
-
portnumber -

Server port. Defaults to 443.

-
-
pingIntervalnumber -

Ping interval in ms. Defaults to 5000.

-
-
pathstring -

The path where your self-hosted PeerServer is running. Defaults to - '/'.

-
-
secureboolean -

true if you're using SSL.Note that our cloud-hosted - server and assets may not support SSL.

-
-
configobject -

Configuration hash passed to RTCPeerConnection. This hash contains any custom - ICE/TURN server configuration. Defaults to - { 'iceServers': [{ 'urls': 'stun:stun.l.google.com:19302' }, { 'urls': 'turn:0.peerjs.com:3478', username: 'peerjs', credential: 'peerjsp' }], 'sdpSemantics': 'unified-plan' } -

-
-
debugnumber -

Prints log messages depending on the debug level passed in. Defaults to - 0.

-
-
0 -

Prints no logs.

-
-
1 -

Prints only errors.

-
-
2 -

Prints errors and warnings.

-
-
3 -

Prints all logs.

-
-
-
-
-
-
-
-
peer.connectmethodconst dataConnection - = peer.connect(id, [options]); -

Connects to the remote peer specified by id and returns a data connection. Be - sure to listen on the error event in case the connection fails.

-
-
idstring -

The brokering ID of the remote peer (their peer.id). -

-
-
[options]object -
-
labelstring -

A unique label by which you want to identify this data connection. If left - unspecified, a label will be generated at random. Can be accessed with dataConnection.label.

-
-
metadata -

Metadata associated with the connection, passed in by whoever initiated the - connection. Can be accessed with dataConnection.metadata. Can be any serializable - type.

-
-
serializationstring -

Can be binary (default), binary-utf8, json, - or none. Can be accessed with dataConnection.serialization.binary-utf8 will take a performance hit because of the way UTF8 strings are - packed into binary format.

-
-
reliableboolean -

Whether the underlying data channels should be reliable (e.g. for large file - transfers) or not (e.g. for gaming or streaming). Defaults to false.Setting reliable to true will use a shim for incompatible browsers (Chrome 30 and below - only) and thus may not offer full performance.

-
-
-
-
-
-
peer.callmethodconst mediaConnection = peer.call(id, stream, [options]); -

Calls the remote peer specified by id and returns a media connection. Be sure - to listen on the error event in case the connection fails.

-
-
idstring -

The brokering ID of the remote peer (their peer.id). -

-
-
streamMediaStream -

The caller's media stream

-
-
[options]object -
-
metadata -

Metadata associated with the connection, passed in by whoever initiated the - connection. Can be accessed with mediaConnection.metadata. Can be any serializable - type.

-
-
sdpTransformmethod -

Function which runs before create offer to modify sdp offer message.

-
-
-
-
-
-
peer.onmethodpeer.on(event, callback); -

Set listeners for peer events.

-
-
'open'eventpeer.on('open', function(id) { ... - }); -

Emitted when a connection to the PeerServer is established. You may use the peer before - this is emitted, but messages to the server will be queued. id is the brokering ID of the peer - (which was either provided in the constructor or assigned by the server).You should not - wait for this event before connecting to other peers if connection speed is important.

-
-
'connection'eventpeer.on('connection', function(dataConnection) { ... }); -

Emitted when a new data connection is established from a remote peer.

-
-
'call'eventpeer.on('call', function(mediaConnection) { ... }); -

Emitted when a remote peer attempts to call you. The emitted - mediaConnection is not yet active; you must first answer the call (mediaConnection.answer([stream]);). Then, you can listen - for the stream event.

-
-
'close'eventpeer.on('close', function() { ... - }); -

Emitted when the peer is destroyed and can no longer accept - or create any new connections. At this time, the peer's connections will all be closed. To - be extra certain that peers clean up correctly, we recommend calling peer.destroy() on a peer - when it is no longer needed.

-
-
'disconnected'eventpeer.on('disconnected', function() { ... }); -

Emitted when the peer is disconnected from the signalling server, either manually or because the connection to the signalling server was lost. When a - peer is disconnected, its existing connections will stay alive, but the peer cannot accept or create any new - connections. You can reconnect to the server by calling peer.reconnect().

-
-
'error'eventpeer.on('error', function(err) { ... - }); -

Errors on the peer are almost always fatal and will destroy the peer. - Errors from the underlying socket and PeerConnections are forwarded here.

These come in the following - err.type flavors:

-
-
'browser-incompatible'Errorfatal -

The client's browser does not support some or all WebRTC features that you are - trying to use.

-
-
'disconnected'Error -

You've already disconnected this peer from the server and can no longer make any - new connections on it.

-
-
'invalid-id'Errorfatal -

The ID passed into the Peer constructor contains illegal characters.

-
-
'invalid-key'Errorfatal -

The API key passed into the Peer constructor contains illegal characters or is not - in the system (cloud server only).

-
-
'network'Error -

Lost or cannot establish a connection to the signalling server.

-
-
'peer-unavailable'Error -

The peer you're trying to connect to does not exist.

-
-
'ssl-unavailable'Errorfatal -

PeerJS is being used securely, but the cloud server does not support SSL. Use a - custom PeerServer.

-
-
'server-error'Errorfatal -

Unable to reach the server.

-
-
'socket-error'Errorfatal -

An error from the underlying socket.

-
-
'socket-closed'Errorfatal -

The underlying socket closed unexpectedly.

-
-
'unavailable-id'Errorsometimes fatal -

The ID passed into the Peer constructor is already taken.This - error is not fatal if your peer has open peer-to-peer connections. This can happen if you attempt to - reconnect a peer that has been disconnected - from the server, but its old ID has now been taken.

-
-
'webrtc'Error -

Native WebRTC errors.

-
-
-
-
-
-
peer.disconnectmethodpeer.disconnect(); -

Close the connection to the server, leaving all existing data and media connections intact. - peer.disconnected will be set to true and the disconnected event will fire.This cannot be - undone; the respective peer object will no longer be able to create or receive any connections and its ID will - be forfeited on the (cloud) server.

-
-
peer.reconnectmethodpeer.reconnect(); -

Attempt to reconnect to the server with the peer's old ID. Only disconnected peers can be reconnected. Destroyed peers cannot be reconnected. If - the connection fails (as an example, if the peer's old ID is now taken), the peer's existing connections will - not close, but any associated errors events will fire.

-
-
peer.destroymethodpeer.destroy(); -

Close the connection to the server and terminate all existing connections. peer.destroyed will be set to true.This - cannot be undone; the respective peer object will no longer be able to create or receive any connections, its - ID will be forfeited on the (cloud) server, and all of its data and media connections will be closed. -

-
-
peer.idstring -

The brokering ID of this peer. If no ID was specified in the - constructor, this will be undefined until the open - event is emitted.

-
-
peer.connectionsobject -

A hash of all connections associated with this peer, keyed by the remote peer's ID.We recommend keeping track of connections yourself rather than relying on this hash.

-
-
peer.disconnectedboolean -

false if there is an active connection to the PeerServer.

-
-
peer.destroyedboolean -

true if this peer and all of its connections can no longer be used.

-
-
DataConnectionclass -

Wraps WebRTC's DataChannel. To get one, use peer.connect or listen for the connect event.

-
-
.sendmethoddataConnection.send(data); -

data is serialized by BinaryPack by default and sent to the remote peer. -

-
-
data -

You can send any type of data, including objects, strings, and blobs.

-
-
-
-
.closemethoddataConnection.close(); -

Closes the data connection gracefully, cleaning up underlying DataChannels and - PeerConnections.

-
-
.onmethoddataConnection.on(event, - callback); -

Set listeners for data connection events.

-
-
'data'eventdataConnection.on('data', function(data) { ... }); -

Emitted when data is received from the remote peer.

-
-
'open'eventdataConnection.on('open', function() { ... }); -

Emitted when the connection is established and ready-to-use.

-
-
'close'eventdataConnection.on('close', function() { ... }); -

Emitted when either you or the remote peer closes the data connection.Firefox does not yet support this event.

-
-
'error'eventdataConnection.on('error', function(err) { ... });
-
-
-
.dataChannelobject -

A reference to the RTCDataChannel object associated with the connection.

-
-
.labelstring -

The optional label passed in or assigned by PeerJS when the connection was initiated. -

-
-
.metadata -

Any type of metadata associated with the connection, passed in by whoever initiated the - connection.

-
-
.openboolean -

This is true if the connection is open and ready for read/write.

-
-
.peerConnectionobject -

A reference to the RTCPeerConnection object associated with the connection.

-
-
.peerstring -

The ID of the peer on the other end of this connection.

-
-
.reliableboolean -

Whether the underlying data channels are reliable; defined when the connection was - initiated.

-
-
.serializationstring -

The serialization format of the data sent over the connection. Can be - binary (default), binary-utf8, json, or none.

-
-
.typestring -

For data connections, this is always 'data'.

-
-
.bufferSizenumber -

The number of messages queued to be sent once the browser buffer is no longer full.

-
-
-
-
MediaConnectionclass -

Wraps WebRTC's media streams. To get one, use peer.call or listen for the call - event.

-
-
.answermethodmediaConnection.answer([stream],[options]); -

When receiving a call event on a peer, you can - call .answer on the media connection provided by the callback to accept the call and optionally - send your own media stream.

-
-
[stream]MediaStream -

A WebRTC media stream from getUserMedia. -

-
-
[options]object -
-
sdpTransformmethod -

Function which runs before create answer to modify sdp answer message.

-
-
-
-
-
-
.closemethodmediaConnection.close(); -

Closes the media connection.

-
-
.onmethodmediaConnection.on(event, - callback); -

Set listeners for media connection events.

-
-
'stream'eventmediaConnection.on('stream', function(stream) { ... }); -

Emitted when a remote peer adds a stream.

-
-
'close'eventmediaConnection.on('close', function() { ... }); -

Emitted when either you or the remote peer closes the media connection. Firefox does not yet support this event.

-
-
'error'eventmediaConnection.on('error', function(err) { ... });
-
-
-
.openboolean -

Whether the media connection is active (e.g. your call has been answered). You can - check this if you want to set a maximum wait time for a one-sided call.

-
-
.metadata -

Any type of metadata associated with the connection, passed in by whoever initiated the - connection.

-
-
.peerstring -

The ID of the peer on the other end of this connection.

-
-
.typestring -

For media connections, this is always 'media'.

-
-
-
-
utilobjectutility -

Provides a variety of helpful utilities.Only the utilities documented - here are guaranteed to be present on util. Undocumented utilities can be removed without warning. - We don't consider these to be 'breaking changes.'

-
-
.browserstringif (util.browser === 'Firefox') { /* OK to peer - with Firefox peers. */ } -

The current browser. This property can be useful in determining whether or not two - peers can connect. For example, as of now data connections are not yet interoperable between major browsers. - util.browser can currently have the values 'Firefox', 'Chrome', 'Unsupported', or 'Supported' - (unknown WebRTC-compatible browser).

-
-
.supportsobjectif (util.supports.data) { /* OK to start a data - connection. */ } -

A hash of WebRTC features mapped to booleans that correspond to whether the feature is - supported by the current browser.Only the properties documented here are guaranteed to be - present on util.supports.

-
-
.audioVideoboolean -

True if the current browser supports media streams and PeerConnection.

-
-
.databoolean -

True if the current browser supports DataChannel and PeerConnection.

-
-
.binaryboolean -

True if the current browser supports binary DataChannels.

-
-
.reliableboolean -

True if the current browser supports reliable DataChannels.

-
-
-
-
-
+
Peerconstructorconst peer = new Peer([id], [options]);

A peer can connect to other peers and listen for connections.

[id]string

Other peers can connect to this peer using the provided ID. If no ID is given, one will be generated by the brokering server. The ID must start and end with an alphanumeric character (lower or upper case character or a digit). In the middle of the ID spaces, dashes (-) and underscores (_) are allowed.It's not recommended that you use this ID to identify peers, as it's meant to be used for brokering connections only. You're recommended to set the metadata option to send other identifying information.

[options]object
keystring

API key for the cloud PeerServer. This is not used for servers other than 0.peerjs.com.PeerServer cloud runs on port 443. Please ensure it is not blocked or consider running your own PeerServer instead.

hoststring

Server host. Defaults to 0.peerjs.com. Also accepts '/' to signify relative hostname.

portnumber

Server port. Defaults to 443.

pingIntervalnumber

Ping interval in ms. Defaults to 5000.

pathstring

The path where your self-hosted PeerServer is running. Defaults to '/'.

secureboolean

true if you're using SSL.Note that our cloud-hosted server and assets may not support SSL.

configobject

Configuration hash passed to RTCPeerConnection. This hash contains any custom ICE/TURN server configuration. Defaults to { 'iceServers': [{ 'urls': 'stun:stun.l.google.com:19302' }, { 'urls': 'turn:0.peerjs.com:3478', username: 'peerjs', credential: 'peerjsp' }], 'sdpSemantics': 'unified-plan' }

debugnumber

Prints log messages depending on the debug level passed in. Defaults to 0.

0

Prints no logs.

1

Prints only errors.

2

Prints errors and warnings.

3

Prints all logs.

peer.connectmethodconst dataConnection = peer.connect(id, [options]);

Connects to the remote peer specified by id and returns a data connection. Be sure to listen on the error event in case the connection fails.

idstring

The brokering ID of the remote peer (their peer.id).

[options]object
labelstring

A unique label by which you want to identify this data connection. If left unspecified, a label will be generated at random. Can be accessed with dataConnection.label.

metadata

Metadata associated with the connection, passed in by whoever initiated the connection. Can be accessed with dataConnection.metadata. Can be any serializable type.

serializationstring

Can be binary (default), binary-utf8, json, or none. Can be accessed with dataConnection.serialization.binary-utf8 will take a performance hit because of the way UTF8 strings are packed into binary format.

reliableboolean

Whether the underlying data channels should be reliable (e.g. for large file transfers) or not (e.g. for gaming or streaming). Defaults to false.Setting reliable to true will use a shim for incompatible browsers (Chrome 30 and below only) and thus may not offer full performance.

peer.callmethodconst mediaConnection = peer.call(id, stream, [options]);

Calls the remote peer specified by id and returns a media connection. Be sure to listen on the error event in case the connection fails.

idstring

The brokering ID of the remote peer (their peer.id).

streamMediaStream

The caller's media stream

[options]object
metadata

Metadata associated with the connection, passed in by whoever initiated the connection. Can be accessed with mediaConnection.metadata. Can be any serializable type.

sdpTransformmethod

Function which runs before create offer to modify sdp offer message.

peer.onmethodpeer.on(event, callback);

Set listeners for peer events.

'open'eventpeer.on('open', function(id) { ... });

Emitted when a connection to the PeerServer is established. You may use the peer before this is emitted, but messages to the server will be queued. id is the brokering ID of the peer (which was either provided in the constructor or assigned by the server).You should not wait for this event before connecting to other peers if connection speed is important.

'connection'eventpeer.on('connection', function(dataConnection) { ... });

Emitted when a new data connection is established from a remote peer.

'call'eventpeer.on('call', function(mediaConnection) { ... });

Emitted when a remote peer attempts to call you. The emitted mediaConnection is not yet active; you must first answer the call (mediaConnection.answer([stream]);). Then, you can listen for the stream event.

'close'eventpeer.on('close', function() { ... });

Emitted when the peer is destroyed and can no longer accept or create any new connections. At this time, the peer's connections will all be closed. To be extra certain that peers clean up correctly, we recommend calling peer.destroy() on a peer when it is no longer needed.

'disconnected'eventpeer.on('disconnected', function() { ... });

Emitted when the peer is disconnected from the signalling server, either manually or because the connection to the signalling server was lost. When a peer is disconnected, its existing connections will stay alive, but the peer cannot accept or create any new connections. You can reconnect to the server by calling peer.reconnect().

'error'eventpeer.on('error', function(err) { ... });

Errors on the peer are almost always fatal and will destroy the peer. Errors from the underlying socket and PeerConnections are forwarded here.

These come in the following err.type flavors:

'browser-incompatible'Errorfatal

The client's browser does not support some or all WebRTC features that you are trying to use.

'disconnected'Error

You've already disconnected this peer from the server and can no longer make any new connections on it.

'invalid-id'Errorfatal

The ID passed into the Peer constructor contains illegal characters.

'invalid-key'Errorfatal

The API key passed into the Peer constructor contains illegal characters or is not in the system (cloud server only).

'network'Error

Lost or cannot establish a connection to the signalling server.

'peer-unavailable'Error

The peer you're trying to connect to does not exist.

'ssl-unavailable'Errorfatal

PeerJS is being used securely, but the cloud server does not support SSL. Use a custom PeerServer.

'server-error'Errorfatal

Unable to reach the server.

'socket-error'Errorfatal

An error from the underlying socket.

'socket-closed'Errorfatal

The underlying socket closed unexpectedly.

'unavailable-id'Errorsometimes fatal

The ID passed into the Peer constructor is already taken.This error is not fatal if your peer has open peer-to-peer connections. This can happen if you attempt to reconnect a peer that has been disconnected from the server, but its old ID has now been taken.

'webrtc'Error

Native WebRTC errors.

peer.disconnectmethodpeer.disconnect();

Close the connection to the server, leaving all existing data and media connections intact. peer.disconnected will be set to true and the disconnected event will fire.This cannot be undone; the respective peer object will no longer be able to create or receive any connections and its ID will be forfeited on the (cloud) server.

peer.reconnectmethodpeer.reconnect();

Attempt to reconnect to the server with the peer's old ID. Only disconnected peers can be reconnected. Destroyed peers cannot be reconnected. If the connection fails (as an example, if the peer's old ID is now taken), the peer's existing connections will not close, but any associated errors events will fire.

peer.destroymethodpeer.destroy();

Close the connection to the server and terminate all existing connections. peer.destroyed will be set to true.This cannot be undone; the respective peer object will no longer be able to create or receive any connections, its ID will be forfeited on the (cloud) server, and all of its data and media connections will be closed.

peer.idstring

The brokering ID of this peer. If no ID was specified in the constructor, this will be undefined until the open event is emitted.

peer.connectionsobject

A hash of all connections associated with this peer, keyed by the remote peer's ID.We recommend keeping track of connections yourself rather than relying on this hash.

peer.disconnectedboolean

false if there is an active connection to the PeerServer.

peer.destroyedboolean

true if this peer and all of its connections can no longer be used.

DataConnectionclass

Wraps WebRTC's DataChannel. To get one, use peer.connect or listen for the connect event.

.sendmethoddataConnection.send(data);

data is serialized by BinaryPack by default and sent to the remote peer.

data

You can send any type of data, including objects, strings, and blobs.

.closemethoddataConnection.close();

Closes the data connection gracefully, cleaning up underlying DataChannels and PeerConnections.

.onmethoddataConnection.on(event, callback);

Set listeners for data connection events.

'data'eventdataConnection.on('data', function(data) { ... });

Emitted when data is received from the remote peer.

'open'eventdataConnection.on('open', function() { ... });

Emitted when the connection is established and ready-to-use.

'close'eventdataConnection.on('close', function() { ... });

Emitted when either you or the remote peer closes the data connection.Firefox does not yet support this event.

'error'eventdataConnection.on('error', function(err) { ... });
.dataChannelobject

A reference to the RTCDataChannel object associated with the connection.

.labelstring

The optional label passed in or assigned by PeerJS when the connection was initiated.

.metadata

Any type of metadata associated with the connection, passed in by whoever initiated the connection.

.openboolean

This is true if the connection is open and ready for read/write.

.peerConnectionobject

A reference to the RTCPeerConnection object associated with the connection.

.peerstring

The ID of the peer on the other end of this connection.

.reliableboolean

Whether the underlying data channels are reliable; defined when the connection was initiated.

.serializationstring

The serialization format of the data sent over the connection. Can be binary (default), binary-utf8, json, or none.

.typestring

For data connections, this is always 'data'.

.bufferSizenumber

The number of messages queued to be sent once the browser buffer is no longer full.

'error'eventdataConnection.on('error', function(err) { ... });
'not-open'Error

An a attempt to use the DataConnection to send a message was made before the connection was open and ready. Listen for the 'open' event to know when a conncetion is ready.

MediaConnectionclass

Wraps WebRTC's media streams. To get one, use peer.call or listen for the call event.

.answermethodmediaConnection.answer([stream],[options]);

When receiving a call event on a peer, you can call .answer on the media connection provided by the callback to accept the call and optionally send your own media stream.

[stream]MediaStream

A WebRTC media stream from getUserMedia.

[options]object
sdpTransformmethod

Function which runs before create answer to modify sdp answer message.

.closemethodmediaConnection.close();

Closes the media connection.

.onmethodmediaConnection.on(event, callback);

Set listeners for media connection events.

'stream'eventmediaConnection.on('stream', function(stream) { ... });

Emitted when a remote peer adds a stream.

'close'eventmediaConnection.on('close', function() { ... });

Emitted when either you or the remote peer closes the media connection. Firefox does not yet support this event.

'error'eventmediaConnection.on('error', function(err) { ... });
.openboolean

Whether the media connection is active (e.g. your call has been answered). You can check this if you want to set a maximum wait time for a one-sided call.

.metadata

Any type of metadata associated with the connection, passed in by whoever initiated the connection.

.peerstring

The ID of the peer on the other end of this connection.

.typestring

For media connections, this is always 'media'.

utilobjectutility

Provides a variety of helpful utilities.Only the utilities documented here are guaranteed to be present on util. Undocumented utilities can be removed without warning. We don't consider these to be 'breaking changes.'

.browserstringif (util.browser === 'firefox') { /* OK to peer with Firefox peers. */ }

The current browser. This property can be useful in determining whether or not two peers can connect. For example, as of now data connections are not yet interoperable between major browsers. util.browser can currently have the values 'firefox', 'chrome', 'safari', 'edge', 'Not a supported browser.', or 'Not a browser.' (unknown WebRTC-compatible agent).

.supportsobjectif (util.supports.data) { /* OK to start a data connection. */ }

A hash of WebRTC features mapped to booleans that correspond to whether the feature is supported by the current browser.Only the properties documented here are guaranteed to be present on util.supports.

.audioVideoboolean

True if the current browser supports media streams and PeerConnection.

.databoolean

True if the current browser supports DataChannel and PeerConnection.

.binaryboolean

True if the current browser supports binary DataChannels.

.reliableboolean

True if the current browser supports reliable DataChannels.

- + \ No newline at end of file From d071d3a33d7f7d0a8f75960d09dcd53aaf83544c Mon Sep 17 00:00:00 2001 From: alonbru Date: Sun, 29 Jan 2023 17:19:21 +0200 Subject: [PATCH 3/3] Ran formatting --- lib/dataconnection.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/dataconnection.ts b/lib/dataconnection.ts index 070d61411..c8b45f66b 100644 --- a/lib/dataconnection.ts +++ b/lib/dataconnection.ts @@ -195,7 +195,7 @@ export class DataConnection this._handleDataMessage({ data }); } } - + /** Emits a typed error message. */ private _emitError(type: DataConnectionErrorType, err: string | Error): void { logger.error("Error:", err);