-
Notifications
You must be signed in to change notification settings - Fork 152
/
signaling-server.js
130 lines (103 loc) · 4.29 KB
/
signaling-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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/**************/
/*** CONFIG ***/
/**************/
const PORT = 8080;
/*************/
/*** SETUP ***/
/*************/
const fs = require("fs");
const express = require('express');
//var http = require('http');
const https = require("https");
const bodyParser = require('body-parser')
const main = express()
//const server = http.createServer(main)
let privateKey, certificate;
privateKey = fs.readFileSync("ssl/server-key.pem", "utf8");
certificate = fs.readFileSync("ssl/server-cert.pem", "utf8");
const credentials = { key: privateKey, cert: certificate };
const server = https.createServer(credentials, main);
const io = require('socket.io').listen(server);
//io.set('log level', 2);
server.listen(PORT, null, function() {
console.log("Listening on port " + PORT);
});
//main.use(express.bodyParser());
main.get('/', function(req, res){ res.sendFile(__dirname + '/client.html'); });
// main.get('/index.html', function(req, res){ res.sendfile('newclient.html'); });
// main.get('/client.html', function(req, res){ res.sendfile('newclient.html'); });
/*************************/
/*** INTERESTING STUFF ***/
/*************************/
var channels = {};
var sockets = {};
/**
* Users will connect to the signaling server, after which they'll issue a "join"
* to join a particular channel. The signaling server keeps track of all sockets
* who are in a channel, and on join will send out 'addPeer' events to each pair
* of users in a channel. When clients receive the 'addPeer' even they'll begin
* setting up an RTCPeerConnection with one another. During this process they'll
* need to relay ICECandidate information to one another, as well as SessionDescription
* information. After all of that happens, they'll finally be able to complete
* the peer connection and will be streaming audio/video between eachother.
*/
io.sockets.on('connection', function (socket) {
socket.channels = {};
sockets[socket.id] = socket;
console.log("["+ socket.id + "] connection accepted");
socket.on('disconnect', function () {
for (var channel in socket.channels) {
part(channel);
}
console.log("["+ socket.id + "] disconnected");
delete sockets[socket.id];
});
socket.on('join', function (config) {
console.log("["+ socket.id + "] join ", config);
var channel = config.channel;
var userdata = config.userdata;
if (channel in socket.channels) {
console.log("["+ socket.id + "] ERROR: already joined ", channel);
return;
}
if (!(channel in channels)) {
channels[channel] = {};
}
for (id in channels[channel]) {
channels[channel][id].emit('addPeer', {'peer_id': socket.id, 'should_create_offer': false});
socket.emit('addPeer', {'peer_id': id, 'should_create_offer': true});
}
channels[channel][socket.id] = socket;
socket.channels[channel] = channel;
});
function part(channel) {
console.log("["+ socket.id + "] part ");
if (!(channel in socket.channels)) {
console.log("["+ socket.id + "] ERROR: not in ", channel);
return;
}
delete socket.channels[channel];
delete channels[channel][socket.id];
for (id in channels[channel]) {
channels[channel][id].emit('removePeer', {'peer_id': socket.id});
socket.emit('removePeer', {'peer_id': id});
}
}
socket.on('part', part);
socket.on('relayICECandidate', function(config) {
var peer_id = config.peer_id;
var ice_candidate = config.ice_candidate;
console.log("["+ socket.id + "] relaying ICE candidate to [" + peer_id + "] ", ice_candidate);
if (peer_id in sockets) {
sockets[peer_id].emit('iceCandidate', {'peer_id': socket.id, 'ice_candidate': ice_candidate});
}
});
socket.on('relaySessionDescription', function(config) {
var peer_id = config.peer_id;
var session_description = config.session_description;
console.log("["+ socket.id + "] relaying session description to [" + peer_id + "] ", session_description);
if (peer_id in sockets) {
sockets[peer_id].emit('sessionDescription', {'peer_id': socket.id, 'session_description': session_description});
}
});
});