forked from vinsonchuong/whiteboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
83 lines (75 loc) · 2.47 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
72
73
74
75
76
77
78
79
80
81
82
83
var express = require('express'),
http = require('http'),
sanitizer = require('validator').sanitize,
sio= require('socket.io'),
fs=require('fs'),
app = express();
// Load site handler
require( __dirname +'/controllers/handler.js')(app);
//Load public static resources like css and images
app.use(express.static( __dirname +'/public'));
//Define template engine. We will use ejs for templates.
app.engine('.html', require('ejs').__express);
app.set('views', __dirname + '/views');
app.set('view engine', 'html');
// Start the server
var server=app.listen(3000, function () {
console.log("Express server listening on port %d",3000);
});
app.io=io=sio.listen(server);
var chat = io.of('/chat');
var canvas = io.of('/canvas');
function sanitize(string) {
return sanitizer(string).entityDecode()
}
io.on('connection', function(socket) {
console.log("socket connected ",socket);
socket.on('setName', function (name) {
console.log("setname recieved",message);
name = sanitize(name);
socket.set('name', name);
socket.broadcast.emit('receive', {
sender:'Server',
message:name + ' has joined.'
})
});
socket.on('send', function (message) {
console.log("send recieved",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) {
console.log("uopdate cursor recieved",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);
}
})*/
socket.broadcast.emit('receive', {
sender:'Server',
message:"server" + ' has left.'
});
socket.broadcast.emit('removeCursor', "server");
});
});