-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.js
105 lines (87 loc) · 2.77 KB
/
ws.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
/*
* Copyright 2014 Richard Larocque
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
var server = require('socket.io')();
var router = require('socket.io-events')();
var moniker = require ('moniker');
var sanitizer = require('sanitizer');
var capitalize = require('capitalize');
var _ = require('underscore');
var namer = moniker.generator([moniker.adjective, moniker.noun], { glue: ' ' });
var roomState = {};
var relay = function(socket, name) {
socket.on(name, function(msg) {
if (!msg || !msg.targetId) {
return;
}
var s = server.sockets.connected[msg.targetId];
if (!s) {
console.error("Message addressed to unknown client.");
} else {
s.emit(name, msg);
}
});
};
server.on('connect', function(socket) {
var id = socket.id;
var room = '';
var initialName = capitalize.words(namer.choose());
var userState = { id: socket.id, name: initialName };
socket.emit('name-offered', initialName);
var broadcast = function(name, data) {
if (!room) { return; }
server.to(room).emit(name, data);
};
var broadcastState = function() {
broadcast('state-update', roomState[room]);
};
var updateState = function() {
if (!room) { return; }
if (!roomState[room]) { roomState[room] = {}; }
roomState[room][id] = userState;
broadcastState();
};
socket.on('join-room', function(roomName) {
room = roomName;
socket.join(room);
socket.emit('id-assigned', socket.id);
updateState();
});
socket.on('mic-ready', function() {
userState.ready = true;
updateState();
});
socket.on('set-name', function(name) {
userState.name = name;
updateState();
});
socket.on('send-message', function(text) {
broadcast('message-sent', {
username: userState.name,
time: (new Date()).getTime(),
text: text
});
});
relay(socket, 'ice-candidate-offer');
relay(socket, 'call-initiation');
relay(socket, 'call-acceptance');
socket.on('disconnect', function() {
if (room) {
delete roomState[room][id];
}
broadcastState();
});
});
module.exports = server;