-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
88 lines (65 loc) · 2.14 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
var version = require('./package.json').version;
var reTrailingSlash = /\/$/;
/**
# rtc-signaller-socket.io
This is a signaller that can be used as a drop-in replacement for
[`rtc-signaller`](https://github.com/rtc-io/rtc-signaller), that
works with a [`socket.io`](http://socket.io) server.
## Example Usage
The following examples show how a client and server can be
configured to work with socket.io, using
[`rtc-quickconnect`](https://github.com/rtc-io/rtc-quickconnect) on
the frontend.
### Server
Run using `node examples/server.js`:
<<< examples/server.js
### Client
Run using `beefy examples/client.js`:
<<< examples/client.js
**/
module.exports = function(socket, opts) {
// create the signaller
var announceTimer;
var signaller = require('rtc-signal/signaller')(opts, bufferMessage);
var queuedMessages = [];
function bufferMessage(message) {
var connected = socket && socket.connected;
if (! connected) {
return queuedMessages.push(message);
}
socket.emit('rtc-signal', message);
}
function init() {
socket.once('connect', function() {
queuedMessages.splice(0).forEach(bufferMessage);
signaller('connected');
});
socket.on('reconnect', function() {
signaller('reconnected');
});
socket.on('disconnect', function() {
signaller('disconnected');
});
socket.on('rtc-signal', signaller._process);
return signaller;
}
signaller.announce = function(data) {
if (socket && socket.connected) {
// always announce on reconnect
signaller.removeListener('connected', signaller._announce);
signaller.on('connected', signaller._announce);
}
signaller._update(data);
clearTimeout(announceTimer);
// send the attributes over the network
return announceTimer = setTimeout(signaller._announce, (opts || {}).announceDelay || 10);
};
signaller.leave = signaller.close = function() {
// Apply the default leave behaviour
signaller._leave();
// Close the socket
return socket && socket.disconnect();
};
signaller._update({ agent: 'signaller-socket.io@' + version });
return init();
};