-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #144 from flowbased/improve_webrtc
Improve WebRTC signalling
- Loading branch information
Showing
9 changed files
with
291 additions
and
184 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
"baseDir": false, | ||
"chai": false, | ||
"noflo": false, | ||
"uuid": false, | ||
"client": false, | ||
"window": false | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
describe('Signaller', () => { | ||
const connectSignaller = (signaller, callback) => { | ||
if (signaller.isConnected()) { | ||
callback(); | ||
return; | ||
} | ||
signaller.once('error', callback); | ||
signaller.once('connected', () => { | ||
signaller.removeListener('error', callback); | ||
callback(); | ||
}); | ||
signaller.connect(); | ||
}; | ||
const disconnectSignaller = (signaller, callback) => { | ||
if (!signaller.isConnected()) { | ||
callback(); | ||
return; | ||
} | ||
signaller.once('error', callback); | ||
signaller.once('disconnected', () => { | ||
signaller.removeListener('error', callback); | ||
callback(); | ||
}); | ||
signaller.disconnect(); | ||
}; | ||
[ | ||
{ | ||
name: 'with default server', | ||
browser: true, | ||
}, | ||
].forEach((variant) => { | ||
if (noflo.isBrowser() && !variant.browser) { | ||
return; | ||
} | ||
|
||
describe(`${variant.name}`, () => { | ||
if (variant.before) { | ||
before(variant.before); | ||
} | ||
if (variant.after) { | ||
after(variant.after); | ||
} | ||
const getSignaller = () => { | ||
if (variant.instantiate) { | ||
return variant.instantiate(); | ||
} | ||
return new client.Signaller(uuid()); | ||
}; | ||
const signaller1 = getSignaller(); | ||
const signaller2 = getSignaller(); | ||
it('should be able to connect', (done) => { | ||
connectSignaller(signaller1, done); | ||
}); | ||
describe('during session', () => { | ||
const roomId = uuid(); | ||
before((done) => { | ||
connectSignaller(signaller2, (err) => { | ||
if (err) { | ||
done(err); | ||
return; | ||
} | ||
signaller2.join(roomId); | ||
setTimeout(done, 100); | ||
}); | ||
}); | ||
after((done) => { | ||
disconnectSignaller(signaller2, done); | ||
}); | ||
it('should be able to join a room', (done) => { | ||
signaller2.once('join', (peer) => { | ||
chai.expect(peer.id).to.equal(signaller1.id); | ||
done(); | ||
}); | ||
signaller1.join(roomId); | ||
}); | ||
it('should be able to receive a DM from a peer', (done) => { | ||
signaller1.once('signal', (signal, peer) => { | ||
chai.expect(signal.hello).to.equal('World'); | ||
chai.expect(peer.id).to.equal(signaller2.id); | ||
done(); | ||
}); | ||
signaller2.signal(signaller1.id, { | ||
hello: 'World', | ||
}); | ||
}); | ||
it('should be able to send a DM to a peer', (done) => { | ||
signaller2.once('signal', (signal, peer) => { | ||
chai.expect(signal.hello).to.equal('NoFlo'); | ||
chai.expect(peer.id).to.equal(signaller1.id); | ||
done(); | ||
}); | ||
signaller1.signal(signaller2.id, { | ||
hello: 'NoFlo', | ||
}); | ||
}); | ||
it('should be able to disconnect', (done) => { | ||
disconnectSignaller(signaller1, done); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,11 @@ | ||
const debug = require('debug')('fbp-protocol-client:platform'); | ||
|
||
const { | ||
EventEmitter, | ||
} = require('events'); | ||
|
||
const isBrowser = () => !((typeof (process) !== 'undefined') && process.execPath && (process.execPath.indexOf('node') !== -1)); | ||
|
||
// Simple compatibility layer between node.js WebSocket client and native browser APIs | ||
// Respects events: open, close, message, error | ||
// Note: no data is passed with open and close events | ||
class NodeWebSocketClient extends EventEmitter { | ||
constructor(address, protocol) { | ||
super(); | ||
const WebSocketClient = require('websocket').client; | ||
this.client = new WebSocketClient(); // the real client | ||
this.connection = null; | ||
|
||
this.client.on('connectFailed', (error) => this.emit('error', error)); | ||
this.client.on('connect', (connection) => { | ||
if (this.connection) { debug('WARNING: multiple connections for one NodeWebSocketClient'); } | ||
this.connection = connection; | ||
connection.on('error', (error) => { | ||
this.connection = null; | ||
this.emit('error', error); | ||
}); | ||
connection.on('close', () => { | ||
this.connection = null; | ||
this.emit('close'); | ||
}); | ||
connection.on('message', (message) => { | ||
this.emit('message', { | ||
...message, | ||
data: message.utf8Data, | ||
}); | ||
}); | ||
|
||
this.emit('open'); | ||
}); | ||
|
||
this.client.connect(address, protocol); | ||
} | ||
|
||
addEventListener(event, listener) { | ||
this.on(event, listener); | ||
} | ||
|
||
close() { | ||
if (!this.connection) { return; } | ||
this.connection.close(); | ||
this.connection = null; | ||
} | ||
|
||
send(msg) { | ||
this.connection.sendUTF(msg); | ||
} | ||
} | ||
|
||
module.exports = { | ||
isBrowser, | ||
EventEmitter, | ||
WebSocket: isBrowser() ? window.WebSocket : NodeWebSocketClient, | ||
WebSocket: isBrowser() ? window.WebSocket : require('websocket').w3cwebsocket, | ||
}; |
Oops, something went wrong.