diff --git a/README.md b/README.md index 78ef11ffba31f79058bcf733819272d06780e630..55c5378eec7b13f7b0039d56791f6b664ff2a402 100644 --- a/README.md +++ b/README.md @@ -65,7 +65,7 @@ webrtc.on('readyToCall', () => { ``` ### Emitting to the hive -Sometimes a peer wants to let every other peer in the room to know about something. This can be accomplished with +Sometimes a peer wants to let every other peer in the room to know about something. This can be accomplished with ```shout(messageType, payload)``` ```js webrtc.shout('taskCompleted', { success: true, id: '137' }); @@ -80,7 +80,7 @@ webrtc.on('receivedPeerData', (type, data, peer) => { ``` ### Communicating with a single peer -Sometimes a peer only wants to send data directly to another peer. This can be accomplished with +Sometimes a peer only wants to send data directly to another peer. This can be accomplished with ```whisper(peer, messageType, payload)``` ```js webrtc.whisper(peer, 'directMessage', { msg: 'Hello world!' }); @@ -114,7 +114,7 @@ webrtc.on('videoAdded', (stream, peer) => { }); ``` -### +### ## Example @@ -154,7 +154,7 @@ class Party extends Component { this.webrtc.on('iceFailed', this.handleConnectionError); this.webrtc.on('connectivityError', this.handleConnectionError); } - + addVideo = (stream, peer) => { this.setState({ peers: [...this.state.peers, peer] @@ -162,25 +162,25 @@ class Party extends Component { this.webrtc.attachStream(stream, this.remoteVideos[peer.id]); }); } - + removeVideo = (video, peer) => { this.setState({ peers: this.state.peers.filter(p => p.id) }); } - + handleConnectionError = (peer) => { const pc = peer.pc; console.log('had local relay candidate', pc.hadLocalRelayCandidate); console.log('had remote relay candidate', pc.hadRemoteRelayCandidate); } - + readyToCall = () => { // Starts the process of joining a room. this.webrtc.joinRoom(this.state.roomID, (err, desc) => { }); } - + // Show fellow peers in the room generateRemotes = () => this.webrtc.getPeers().map((p) => ( <div key={p.id}> @@ -196,7 +196,7 @@ class Party extends Component { <p>{p.nick}</p> </div> )); - + disconnect = () => { this.webrtc.stopLocalVideo(); this.webrtc.leaveRoom(); @@ -206,7 +206,7 @@ class Party extends Component { componentWillUnmount() { this.disconnect(); } - + render() { return ( <div> @@ -227,7 +227,7 @@ class Party extends Component { } export default Party; - + ``` ## API @@ -238,7 +238,7 @@ export default Party; - `object options` - `string url` - *optional* url for your socket.io signaling server - - `bool debug` - *optional* logs all webrtc events + - `bool debug` - *optional* logs all webrtc events - `string nick` - *optional* sets your nickname. Peers' nicknames can be accessed with `peer.nick` - `[string|DomElement|Ref] localVideoEl` - Can be a ref, DOM element, or ID of the local video - `bool autoRequestMedia` - *optional(=true)* automatically request @@ -296,6 +296,11 @@ this.webrtc.on('receivedPeerData', (type, payload, peer) => { - `payload` any kind of data sent by the peer, usually an object - `peer` the object representing the peer and its peer connection +`'receivedSignalData', type, payload, peer` - emitted when a peer sends data via `broadcast` +- `type` a label, usually a string, that describes the payload +- `payload` any kind of data sent by the peer, usually an object +- `peer` the object representing the peer and its peer connection + `'createdPeer', peer` - this will be emitted when: - joining a room with existing peers, once for each peer - a new peer joins your room @@ -343,26 +348,20 @@ in the config `resume()` - resumes sending video and audio to your peers -`shout(messageType, payload)` - broadcasts a message +`shout(messageType, payload)` - sends a message to all peers in the room via the default data channel -- `string messageType` - A value that represents the classification of the payload +- `string messageType` - An arbitrary value that represents the classification of the payload - `object payload` - an arbitrary value or object to send to peers `whisper(peer, messageType, payload)` - sends a message to a single peer in the room via the default data channel -- `string messageType` - A value that represents the classification of the payload -- `object payload` - an arbitrary value or object to send to peers - -`sendToAll(messageType, payload)` - broadcasts a message to all peers in the -room via the signaling server - -- `string messageType` - The event label that be broadcasted via the signaling server +- `string messageType` - An arbitrary value that represents the classification of the payload - `object payload` - an arbitrary value or object to send to peers -`sendDirectlyToAll(messageType, payload, channel)` - broadcasts a message -to all peers in the room via a data channel +`broadcast(messageType, payload)` - broadcasts a message to all peers in the +room via the signaling server (similar to `shout`, but not p2p) -- `string messageType` - the event label that peers will listen for -- `object payload` - an arbitrary value or object +`sendDirectlyToAll(messageType, payload, channel)` - sends a message +to all peers in the room via a data channel (same as `shout`, except you can specify your own data channel. Use this if you need to set up a new data channel) - `string channel` - (optional) the name of the data channel `getPeers(sessionId, type)` - returns all peers by `sessionId` and/or `type` diff --git a/src/liowebrtc.js b/src/liowebrtc.js index 5ef75a3949c8c5c31e3597772eb2340f70f9ddb9..618bcc5deb4160b1a63b5fa13a34efde23ea6012 100644 --- a/src/liowebrtc.js +++ b/src/liowebrtc.js @@ -126,7 +126,7 @@ class LioWebRTC extends WildEmitter { this.webrtc = new WebRTC(opts); // attach a few methods from underlying lib to liowebrtc. - ['mute', 'unmute', 'pauseVideo', 'resumeVideo', 'pause', 'resume', 'sendToAll', 'sendDirectlyToAll', 'getPeers', 'shout', 'whisper'].forEach((method) => { + ['mute', 'unmute', 'pauseVideo', 'resumeVideo', 'pause', 'resume', 'sendToAll', 'sendDirectlyToAll', 'getPeers', 'shout', 'whisper', 'broadcast'].forEach((method) => { self[method] = self.webrtc[method].bind(self.webrtc); }); diff --git a/src/peer.js b/src/peer.js index 47dc98521da686f2a0b8a2d23319a453156fca47..9774fea9fc33834d9a4ea5d178910750e3be4d41 100644 --- a/src/peer.js +++ b/src/peer.js @@ -139,10 +139,12 @@ class Peer extends WildEmitter { mLine.iceTransport.addRemoteCandidate({}); } }); + } else if (message.type === 'signalData') { + this.parent.emit('receivedSignalData', message.payload.type, message.payload.payload, self); } } - // send via signalling channel + // send via signaling channel send(messageType, payload) { const message = { to: this.id, diff --git a/src/webrtc.js b/src/webrtc.js index 05aa0b2ec90cad69aa3c660c84f6417e544a3ed5..8df366d3375ebb5b3f289b116e3edbe748ff2978 100644 --- a/src/webrtc.js +++ b/src/webrtc.js @@ -142,12 +142,16 @@ class WebRTC extends LocalMedia { }); } - shout(messageLabel, payload) { - this.sendDirectlyToAll(messageLabel, payload, 'liowebrtc'); + shout(messageType, payload) { + this.sendDirectlyToAll(messageType, payload, 'liowebrtc'); } - whisper(peer, messageLabel, payload) { - peer.sendDirectly(messageLabel, payload); + whisper(peer, messageType, payload) { + peer.sendDirectly(messageType, payload); + } + + broadcast(messageType, payload) { + this.sendToAll('signalData', { type: messageType, payload }); } }