Skip to content

Commit 38090d3

Browse files
committed
Server changes to append hostname to the room
1 parent 3f3d3e8 commit 38090d3

File tree

1 file changed

+28
-20
lines changed

1 file changed

+28
-20
lines changed

server.js

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ app.use(express.static(path.join(__dirname, 'www')));
1111
// Get PORT from env variable else assign 3000 for development
1212
const PORT = process.env.PORT || 3000;
1313
server.listen(PORT, null, function() {
14-
console.log("Listening on port " + PORT);
14+
console.log('Listening on port ' + PORT);
1515
});
1616

1717
// All URL patterns should served with the same file.
@@ -20,22 +20,24 @@ app.get(['/', '/:room'], (req, res) => res.sendFile(path.join(__dirname, 'www/in
2020
const channels = {};
2121
const sockets = {};
2222

23-
io.sockets.on('connection', (socket) => {
23+
io.sockets.on('connection', socket => {
24+
const socketHostName = socket.handshake.headers.host.split(':')[0];
25+
2426
socket.channels = {};
2527
sockets[socket.id] = socket;
2628

27-
console.log("[" + socket.id + "] connection accepted");
29+
console.log('[' + socket.id + '] connection accepted');
2830
socket.on('disconnect', () => {
2931
for (const channel in socket.channels) {
3032
part(channel);
3133
}
32-
console.log("[" + socket.id + "] disconnected");
34+
console.log('[' + socket.id + '] disconnected');
3335
delete sockets[socket.id];
3436
});
3537

36-
socket.on('join', (config) => {
37-
console.log("[" + socket.id + "] join ", config);
38-
const channel = config.channel;
38+
socket.on('join', config => {
39+
console.log('[' + socket.id + '] join ', config);
40+
const channel = socketHostName + config.channel;
3941

4042
// Already Joined
4143
if (channel in socket.channels) return;
@@ -45,44 +47,50 @@ io.sockets.on('connection', (socket) => {
4547
}
4648

4749
for (id in channels[channel]) {
48-
channels[channel][id].emit('addPeer', { 'peer_id': socket.id, 'should_create_offer': false });
49-
socket.emit('addPeer', { 'peer_id': id, 'should_create_offer': true });
50+
channels[channel][id].emit('addPeer', { peer_id: socket.id, should_create_offer: false });
51+
socket.emit('addPeer', { peer_id: id, should_create_offer: true });
5052
}
5153

5254
channels[channel][socket.id] = socket;
5355
socket.channels[channel] = channel;
5456
});
5557

56-
const part = (channel) => {
58+
const part = channel => {
5759
// Socket not in channel
5860
if (!(channel in socket.channels)) return;
5961

6062
delete socket.channels[channel];
6163
delete channels[channel][socket.id];
6264

6365
for (id in channels[channel]) {
64-
channels[channel][id].emit('removePeer', { 'peer_id': socket.id });
65-
socket.emit('removePeer', { 'peer_id': id });
66+
channels[channel][id].emit('removePeer', { peer_id: socket.id });
67+
socket.emit('removePeer', { peer_id: id });
6668
}
67-
}
69+
};
6870

69-
socket.on('relayICECandidate', (config) => {
71+
socket.on('relayICECandidate', config => {
7072
let peer_id = config.peer_id;
7173
let ice_candidate = config.ice_candidate;
72-
console.log("[" + socket.id + "] relay ICE-candidate to [" + peer_id + "] ", ice_candidate);
74+
console.log('[' + socket.id + '] relay ICE-candidate to [' + peer_id + '] ', ice_candidate);
7375

7476
if (peer_id in sockets) {
75-
sockets[peer_id].emit('iceCandidate', { 'peer_id': socket.id, 'ice_candidate': ice_candidate });
77+
sockets[peer_id].emit('iceCandidate', { peer_id: socket.id, ice_candidate: ice_candidate });
7678
}
7779
});
7880

79-
socket.on('relaySessionDescription', (config) => {
81+
socket.on('relaySessionDescription', config => {
8082
let peer_id = config.peer_id;
8183
let session_description = config.session_description;
82-
console.log("[" + socket.id + "] relay SessionDescription to [" + peer_id + "] ", session_description);
84+
console.log(
85+
'[' + socket.id + '] relay SessionDescription to [' + peer_id + '] ',
86+
session_description
87+
);
8388

8489
if (peer_id in sockets) {
85-
sockets[peer_id].emit('sessionDescription', { 'peer_id': socket.id, 'session_description': session_description });
90+
sockets[peer_id].emit('sessionDescription', {
91+
peer_id: socket.id,
92+
session_description: session_description
93+
});
8694
}
8795
});
88-
});
96+
});

0 commit comments

Comments
 (0)