-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello-server.js
60 lines (34 loc) · 1.34 KB
/
hello-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
var http = require('http'),
fs = require('fs'),
// NEVER use a Sync function except at start-up!
index = fs.readFileSync(__dirname + '/hello-multiline.html');
// Send index.html to all requests
var app = http.createServer(function(req, res) {
res.writeHead(200, {'Content-Type': 'text/html'});
res.end(index);
});
// Socket.io server listens to our app
var io = require('socket.io').listen(app);
var connectedClients = [];
io.on('connection', function(socket) {
//Add to clinets pool
connectedClients.push( { socketId: socket.id } );
io.emit('connectedClients', { connectedClients: connectedClients });
socket.emit('getSocketId', { socketId: socket.id } )
socket.on('setSelectionRange', function( data ) {
console.log(data)
socket.broadcast.emit('getSelectionRange', data);
});
socket.on('disconnect', function ( ) {
socket.broadcast.emit('removeClient', socket.id);
console.log('user disconnected', socket.id);
for (var i = connectedClients.length - 1; i >= 0; i--) {
if( connectedClients[i].socketId === socket.id) {
connectedClients.splice(i, 1);
io.emit('connectedClients', { connectedClients: connectedClients });
break;
}
};
});
});
app.listen(3000);