-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathserver.js
More file actions
44 lines (31 loc) · 834 Bytes
/
Copy pathserver.js
File metadata and controls
44 lines (31 loc) · 834 Bytes
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
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(8000);
io.set('log level', 1);
function handler (req, res) {
var filePath = req.url;
if (filePath == '/')
filePath = '/index.html';
fs.readFile(__dirname + filePath,
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (client) {
console.log(client.id);
client.on('move', function(message) {
client.broadcast.emit('move', message);
});
client.on('silent',function(){
client.broadcast.emit('silent',client.id);
});
client.on('disconnect', function(){
io.sockets.emit('close', client.id);
});
});