-
Notifications
You must be signed in to change notification settings - Fork 32
/
rtc_server.js
executable file
·80 lines (67 loc) · 2.44 KB
/
rtc_server.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
#!/usr/bin/env node
var opts = require('minimist')(process.argv.slice(2),
{default: {port: 8001,
home: 'rtc.html'}});
var express = require('express');
var app = express();
var ExpressPeerServer = require('peer').ExpressPeerServer;
// PeerJS default options
var options = {
debug: true
}
// Create PeerJS server channel
function newPeerServerChannel(channel) {
var peerServer = ExpressPeerServer(server, options);
// Attach PeerJS server to Express server
app.use('/api/' + channel, peerServer);
// PeerJS event handlers
peerServer.on('connection', function (id) {
console.log("connection - channel: " + channel + ", ID:", id);
console.log("peers:", Object.keys( peerServer._clients.peerjs));
});
peerServer.on('disconnect', function (id) {
console.log("disconnect - channel: " + channel + ", ID:", id);
console.log("peers:", Object.keys( peerServer._clients.peerjs));
});
app.get('/peers/' + channel, function(req, res, next) {
res.send(JSON.stringify(Object.keys( peerServer._clients.peerjs)));
});
}
var channelCnt = 0;
var channels = {};
app.get('/' + opts.home, function(req, res, next) {
var channel = null;
// Pass along query parameters
var query = "";
for (var k in req.query) {
query += '&'+k+'='+req.query[k];
if (k === 'channel') {
channel = req.query[k];
}
}
if (channel === null || (!(channel in channels))) {
// Redirect to make sure URL has channel parameter and is also
// marked as the firstServer
if (channel === null) {
// Assign an unused channel number
do {
channel = channelCnt++;
} while (channel in channels);
var url = '/' + opts.home + '?channel=' + channel + query + '#firstServer';
} else {
var url = '/' + opts.home + '?' + query + '#firstServer';
}
// Create a new channel
channels[channel] = newPeerServerChannel(channel);
// Do the redirect
res.send('<html><head><meta http-equiv="refresh" content="0; url=' + url + '"/></head></html>');
} else {
// The channel already exists, continue
next();
}
});
// Static file serving
app.use(express.static('./'));
// Start Express server
var server = app.listen(opts.port);
console.log("Server started on port " + opts.port);