-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
195 lines (169 loc) · 4.26 KB
/
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
const PORT = process.env.PORT || 5000;
// Utilities
const nanoid = require('nanoid').nanoid;
require('colors');
const http = require('http');
const WebSocket = require('ws');
const express = require('express');
const app = express();
const server = http.createServer(app);
const wss = new WebSocket.Server({ server });
const sockets = {};
const channels = {};
// Listen to HTTP requests
server.listen(PORT, '127.0.0.1', () => {
console.log(`Listening at ${PORT}...`);
});
server.on('error', error => {
console.log(error);
});
// Manage HTTP requests using Express
app.get('/', (req, res) => {
res.sendFile(__dirname + '/client.html');
});
wss.on('error', error => {
console.log(error);
});
// Manage WebSocket connections
wss.on('connection', socket => {
socket.id = nanoid();
socket.channels = {};
sockets[socket.id] = socket;
socket.on('close', () => {
for (var channel in socket.channels) {
part(channel);
}
console.log(`[${socket.id}] has disconnected.`.red);
delete sockets[socket.id];
console.log(
`${Object.keys(sockets).length} ${Object.keys(sockets)}`.yellow
);
});
socket.on('message', message => {
message = JSON.parse(message);
switch (message.type) {
case undefined || false:
console.log('No message type provided by client -', message);
break;
case 'join':
console.log(
`${socket.id} wants to join channel ${
message.channel
}, he sent the following userData: ${JSON.stringify(
message.userData
)}`.blue
);
var channel = message.channel;
var userData = message.userData;
if (channel in socket.channels) {
console.log(
`[${socket.id}] ERROR: already exists in ${channel}`.red
);
return;
}
if (!(channel in channels)) {
channels[channel] = {};
}
for (id in channels[channel]) {
// Connect newly created peer to existing peers
channels[channel][id].send(
JSON.stringify({
type: 'addPeer',
config: {
peer_id: socket.id,
should_create_offer: false,
},
})
);
// Connect existing peers to our newly created peer
socket.send(
JSON.stringify({
type: 'addPeer',
config: { peer_id: id, should_create_offer: true },
})
);
}
channels[channel][socket.id] = socket;
socket.channels[channel] = channel;
console.log(
`${Object.keys(sockets).length} ${Object.keys(sockets)}`
.yellow
);
// 'join' handler break
break;
case 'part':
part(message.channel);
// 'part' handler break
break;
case 'relayICECandidate':
var peer_id = message.peer_id;
var ice_candidate = message.ice_candidate;
console.log(
`[${socket.id}] relaying ICE candidate to [${peer_id}]`
.underline.blue
// ice_candidate
);
if (peer_id in sockets) {
sockets[peer_id].send(
JSON.stringify({
type: 'iceCandidate',
peer_id: socket.id,
ice_candidate,
})
);
}
// 'relayICECandidate' handler break
break;
case 'relaySessionDescription':
var peer_id = message.peer_id;
var session_description = message.session_description;
console.log(
`[${socket.id}] relaying session description to [${peer_id}]`
.underline.blue
// session_description
);
if (peer_id in sockets) {
sockets[peer_id].send(
JSON.stringify({
type: 'sessionDescription',
peer_id: socket.id,
session_description: session_description,
})
);
}
// 'relaySessionDescription' handler break
break;
default:
console.log(
'Unknown message type received from client -',
message.type
);
break;
}
});
// Utility function "part"
const part = channel => {
console.log(`[${socket.id}] wants to part channel ${channel}`.blue);
if (!(channel in socket.channels)) {
console.log(`[${socket.id}] ERROR: not in ${channel}`.red);
return;
}
delete socket.channels[channel];
delete channels[channel][socket.id];
for (id in channels[channel]) {
channels[channel][id].send(
JSON.stringify({
type: 'removePeer',
peer_id: socket.id,
})
);
socket.send(
JSON.stringify({
type: 'removePeer',
peer_id: id,
})
);
}
};
console.log(`[${socket.id}] has connected.`.green);
});