Skip to content
This repository has been archived by the owner on May 13, 2021. It is now read-only.

Commit

Permalink
Use electron-webrtc instead of node-webrtc
Browse files Browse the repository at this point in the history
  • Loading branch information
getkey committed Jan 14, 2017
1 parent b12e84f commit 8e02928
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 15 deletions.
4 changes: 2 additions & 2 deletions examples/slave.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ new enslavism.Slave('ws://localhost:8081', {
slave.on('connection', clCo => {
clCo.on('datachannel', dc => {
console.log('new dataChannel');
dc.addEventListener('open', ev => {
dc.on('open', ev => {
console.log('data channel open', ev);
dc.send('hallo welt');
});
dc.addEventListener('message', msg => {
dc.on('message', msg => {
console.log('Received message:', msg.data);
});
});
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"game"
],
"dependencies": {
"wrtc": "0.0.x",
"electron-webrtc": "0.2.x",
"ws": "1.x",
"ipaddr.js": "1.x",
"cookie": "0.x",
Expand Down
10 changes: 6 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,22 @@ Basically, you have:

## The point

* transmiting encrypted data without having to register a SSL certificate (unlike secure WebSockets)
* transmitting encrypted data without having to register a SSL certificate (unlike secure WebSockets)
* configurable reliability (unreliable is fast!)
* configurable delivery ordering (unordered is fast!)
* an architecture that allows browser clients to choose which independent server to connect to (useful for games)


## Examples
## Try the examples

```sh
$ npm install
$ node example/master.js
$ node example/slave.js # in a different terminal
```

Enslavism depends [on](https://github.com/mappum/electron-webrtc/pull/91) [patches](https://github.com/mappum/electron-webrtc/pull/92) that aren't yet merged into [electron-webrtc](https://github.com/mappum/electron-webrtc).

Now open your browser at `http://localhost:8081/`.


Expand Down Expand Up @@ -276,11 +278,11 @@ Triggered each time a client creates a datachannel.
clientCo.on('datachannel', dc => {
console.log('new dataChannel', dc);

dc.addEventListener('open', ev => { // triggered once the datachannel is open
dc.on('open', ev => { // triggered once the datachannel is open
console.log('data channel open', ev);
dc.send('hallo welt');
});
dc.addEventListener('message', msg => { // triggered when receiving a message from a client
dc.on('message', msg => { // triggered when receiving a message from a client
console.log(msg);
});
});
Expand Down
10 changes: 5 additions & 5 deletions server/client_connection.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as proto from '../shared/proto.js';

const EventEmitter = require('events'),
webrtc = require('wrtc');
webrtc = require('electron-webrtc')();

export default class ClientConnection extends EventEmitter {
constructor(id, sdp, slave) {
Expand All @@ -12,13 +12,13 @@ export default class ClientConnection extends EventEmitter {
this.dataChannels = {};

this.clientCon = new webrtc.RTCPeerConnection();
this.clientCon.onicecandidate = (iceEv) => {
this.clientCon.on('icecandidate', iceEv => {
if (!iceEv.candidate) return;
this.slave.ws.send(proto.iceCandidateToClient.serialize(id, iceEv.candidate));
};
this.clientCon.ondatachannel = ev => {
});
this.clientCon.on('datachannel', ev => {
this.emit('datachannel', ev.channel);
};
});

let desc = new webrtc.RTCSessionDescription({
type: 'offer',
Expand Down
5 changes: 2 additions & 3 deletions server/slave.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as convert from './convert.js';
import ClientConnection from './client_connection.js';

const EventEmitter = require('events'),
webrtc = require('wrtc'),
webrtc = require('electron-webrtc')(),
WebSocket = require('ws'),
cookie = require('cookie');

Expand Down Expand Up @@ -50,8 +50,7 @@ export default class Slave extends EventEmitter {
let {id, sdpMid, sdpMLineIndex, candidate} = proto.iceCandidateFromClient.deserialize(msg);
let receiver = this.findClient(id);
if (receiver !== undefined) {
receiver.clientCon.addIceCandidate(new webrtc.RTCIceCandidate({candidate, sdpMid, sdpMLineIndex}))
.catch(e => {
receiver.clientCon.addIceCandidate(new webrtc.RTCIceCandidate({candidate, sdpMid, sdpMLineIndex})).catch(e => {
console.error('adding ICE candidate: failure', e, id, sdpMid, sdpMLineIndex, candidate);
});
}
Expand Down

0 comments on commit 8e02928

Please sign in to comment.