forked from pizuricv/webRTC-over-websockets
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwebsocket-server.js
More file actions
195 lines (171 loc) · 7.08 KB
/
Copy pathwebsocket-server.js
File metadata and controls
195 lines (171 loc) · 7.08 KB
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
var WebSocketServer = require('websocket').server;
var http = require('http');
var fs = require('fs');
var people = {};
var connectionDict = {};
var rooms = {};
var numberOfConnections = 0;
var roster = {};
var settings = {
websocketPort: 1337,
refreshRate : 5000,
autoCall : true,
acceptNewUsers : false
}
fs.readFile(process.argv[2] || './settings.json', function(err, data) {
if (err) {
console.log('No settings.json found ('+err+'). Using default settings');
} else {
settings = JSON.parse(data.toString('utf8', 0, data.length));
}
console.log(settings);
});
fs.readFile('data/people.json', 'utf8', function (err, data) {
if (err) throw err;
var obj = JSON.parse(data.toString('utf8', 0, data.length));
roster.people = obj;
for(var i=0; i< obj.length; i ++){
people[obj[i].id] = 'off';
};
console.log('Loaded ' + obj.length + ' people from people.json file');
});
fs.readFile('data/rooms.json', 'utf8', function (err, data) {
if (err) throw err;
var obj = JSON.parse(data.toString('utf8', 0, data.length));
roster.rooms = obj;
console.log('Loaded ' + obj.length + ' rooms from rooms.json file');
});
var server = http.createServer(function(request, response) {
// process HTTP request. Since we're writing just WebSockets serve we don't have to implement anything.
}).listen(settings.websocketPort, function() {
console.log('Socket server is listening on port ' + settings.websocketPort);
});
wsServer = new WebSocketServer({
httpServer: server
});
setInterval(sendPresence, settings.refreshRate);
// This callback function is called every time someone tries to connect to the WebSocket server
// if the type of message is presence, it will send the broadcast. If the type is room, it will send the offer type as multicast,
// if from and to field are defined, and connection.name that maches to exists, it will send the unicast.
// Otherwise, it will send the broadcast.
wsServer.on('request', function(request) {
numberOfConnections ++;
console.log('Connection from origin ' + request.origin);
var connection = request.accept(null, request.origin);
console.log('Connection address ' + connection.remoteAddress);
console.log('Number of connections ' + numberOfConnections);
connection.on('message', function(message) {
if (message.type === 'utf8') {
// process WebSocket message
console.log('Received Message ' + message.utf8Data);
//parse the message
msg = JSON.parse(message.utf8Data);
var type = msg.type;
var from = msg.from;
var to = msg.to;
var name;
//accept only messages that are authorized, in simple case, we assume that the
//first call is the presence with a name of the caller
if(type !== 'presence' && connection.name === undefined){
console.log('connection not allowed, name not provided');
return;
}
//after presence message, socket connection is 'named', only such connections participate later.
if(type === 'presence'){
name = msg.name;
if(people[name] === undefined && !settings.acceptNewUsers){
console.log('Unknown user not allowed');
return;
}
var status = msg.status;
if(status === 'on'){
connection.name = name;
connectionDict[name] = connection;
console.log('adding '+ name)
people[name] = 'on';
if(settings.sendRoster){
connection.send(JSON.stringify({type: "roster", people: roster.people, rooms: roster.rooms}));
}
} else {
remove(name);
}
} else if(type === 'room'){
if(rooms[to] === undefined)
rooms[to] = [];
console.log('Number of people in the room ' + rooms[to].length);
console.log('Sending multicast from ' + from + ": to room "+ to);
if(settings.autoCall){
for(var i = 0; i < rooms[to].length; i ++){
var x = rooms[to][i];
if(connectionDict[x] !== undefined){
console.log('Sending offer from ' + from + ": to "+ x);
sendOffer(connectionDict[x], from, x);
}
}
}
if(rooms[to].indexOf(from) < 0)
rooms[to].push(from);
return;
} else if(to !== undefined && from !== undefined){
if(connectionDict[to] !== undefined && people[to] !== 'off'){
console.log('Sending unicast from ' + from + ":"+ to);
connectionDict[to].send(message.utf8Data, sendCallback);
}
} else {
console.log("message couldn't be passed to " + to);
}
return;
}
console.log('Sending broadcast from '+ from);
// broadcast message to all clients that have name attached
for(var client in connectionDict){
if(client !== name){
console.log('Sending data to '+ client);
connectionDict[client].send(message.utf8Data, sendCallback);
}
}
});
connection.on('close', function(conn) {
console.log('Peer disconnected.');
numberOfConnections --;
remove(connection.name);
});
});
function sendOffer(connection, _from, _to){
connection.send(JSON.stringify({type: 's-offer', from: _from, to: _to}), sendCallback);
}
function sendPresence(){
for(var _name in people){
for(var client in connectionDict){
if(people[_name] === 'off'){
console.log('Person '+ _name + '[off] -> ' + client);
connectionDict[client].send(JSON.stringify({type: 'presence', name: _name, status: 'off'}), sendCallback);
} else if(people[_name] === 'on'){
console.log('Person '+ _name + '[on] -> ' + client);
connectionDict[client].send(JSON.stringify({type: 'presence', name: _name, status: 'on'}), sendCallback);
}
}
}
for(var room in rooms){
for(var i = 0; i < rooms[room].length; i ++){
var client = rooms[room][i];
if(connectionDict[client] !== undefined){
console.log('Room '+ room + ' -> ' + client);
connectionDict[client].send(JSON.stringify({type: 'presence', name: room,
status: 'on', room: true}), sendCallback);
}
}
}
}
function remove(name){
if(name !== undefined && name !== null){
console.log('removing '+ name);
people[name] = 'off';
delete connectionDict[name];
}
}
function sendCallback(err) {
if (err){
console.error("send() error: " + err);
}
}