This repository has been archived by the owner on Jan 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
server.js
71 lines (61 loc) · 1.86 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
64
65
66
67
68
69
70
71
var
port = +process.argv[2] || 8080,
sanitizer = require('validator').sanitize,
express = require('express'),
server = express.createServer(),
io = require('socket.io').listen(server),
chat = io.of('/chat'),
canvas = io.of('/canvas')
;
function sanitize(string) {
return sanitizer(string).entityDecode()
}
server.listen(port);
server.get(/(^\/.*$)/, function(request, response) {
var fileName = request.params[0];
if (fileName == '/')
fileName = '/index.html';
response.sendfile(__dirname + '/client' + fileName);
});
io.sockets.on('connection', function(socket) {
socket.on('setName', function (name) {
name = sanitize(name);
socket.set('name', name);
socket.broadcast.emit('receive', {
sender:'Server',
message:name + ' has joined.'
})
});
socket.on('send', function (message) {
socket.get('name', function(error, name) {
if (name)
socket.broadcast.emit('receive', {
sender:name,
message:sanitize(message)
})
})
});
socket.on('draw', function (command) {
io.sockets.emit('draw', command)
});
socket.on('updateCursor', function(position) {
socket.get('name', function(error, name) {
if (name)
socket.broadcast.emit('updateCursor', {
name:name,
position:position
});
});
});
socket.on('disconnect', function() {
socket.get('name', function(error, name) {
if (name) {
socket.broadcast.emit('receive', {
sender:'Server',
message:name + ' has left.'
});
socket.broadcast.emit('removeCursor', name);
}
})
});
});