-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.js
108 lines (77 loc) · 2.54 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
/* jshint node: true */
'use strict';
var debug = require('debug')('rtc-switchboard');
/**
# rtc-switchboard
This is an rtc.io signalling server (counterpart to
[rtc-signaller](https://github.com/rtc-io/rtc-signaller)) uses websockets to
communicate with signalling clients. It has been designed and built
primarily as a _reference implementation_ for a signalling server and is
not designed to be deployed at scale.
## Try it out
If you would like to our test signalling server (no uptime guaranteed) then
you can use [rtc-quickconnect](https://github.com/rtc-io/rtc-quickconnect)
and take it for a spin:
<<< examples/try-switchboard.js
Other examples are available in the [guidebook](http://guidebook.rtc.io)
## Usage: Standalone
If you wish to use `rtc-switchboard` on it's own to test signalling,
then you can simply clone this repository, install dependencies and start
the server:
```
git clone https://github.com/rtc-io/rtc-switchboard.git
cd rtc-switchboard
npm install && npm start
```
If you wish to run the server on a specific port, then set the `NODE_PORT`
environment variable prior to execution:
```
NODE_PORT=8997 node server.js
```
## Usage: API
To create an application using switchboard signalling, see the following
examples:
### Pure Node HTTP
<<< server.js
### Using Express
<<< examples/express.js
## Usage: Docker
If you are interested in deploying an instance of `rtc-switchboard` using
[docker](https://www.docker.com/) then the following is a great place to
start:
<https://github.com/synctree/docker-rtc-switchboard>
<<< docs/logging.md
**/
module.exports = function(server, opts) {
var WebSocketServer = require('ws').Server;
var wss = new WebSocketServer({ server: server });
var board = require('rtc-switch')();
var connections = [];
wss.on('connection', function connection(ws) {
var peer = board.connect();
// add the socket to the connection list
connections.push(ws);
ws.on('message', peer.process);
peer.on('data', function(data) {
if (ws.readyState === 1) {
debug('<== %s %s', peer.id, data);
ws.send(data);
}
});
ws.on('close', function() {
// trigger the peer leave
peer.leave();
// splice out the connection
connections = connections.filter(function(conn) {
return conn !== ws;
});
});
});
// add a reset helper
board.reset = function() {
connections.splice(0).forEach(function(conn) {
conn.close();
});
};
return board;
};