-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
63 lines (45 loc) · 1.55 KB
/
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
60
61
62
63
var nodeStatic = require('node-static'),
http = require('http'),
socketIo = require('socket.io'),
PORT = 8080,
fileServer,
httpServer,
io;
// Create a node-static server instance to serve the './public' folder
fileServer = new nodeStatic.Server('./public');
httpServer = http.createServer(function(request, response){
request.addListener('end', function(){
fileServer.serve(request, response, function(e, res){
if (e && e.status === 404){
//file wasn't found
response.writeHead(404);
response.write('<h1>404 file not found</h1>');
response.end();
}
});
});
});
httpServer.listen(PORT);
// socket init
io = socketIo.listen(httpServer);
io.sockets.on('connection', function(socket){
var sessionId = socket.id;
socket.emit('welcome', {id: sessionId}); //unused
socket.on('user_new_path', function(msg){
socket.broadcast.emit('new_path', { point: msg.point,
id: sessionId });
});
socket.on('user_add_point_to_path', function(msg){
socket.broadcast.emit('add_point_to_path',{ delta: msg.delta,
middlePoint: msg.middlePoint,
id: sessionId });
});
socket.on('user_end_path', function(msg){
socket.broadcast.emit('end_path', { point: msg.point,
id: sessionId});
});
socket.on('disconnect', function(){
socket.emit('user disconnected');
});
});
console.log("http listening on " + PORT);