-
Notifications
You must be signed in to change notification settings - Fork 4
/
sketch.js
63 lines (50 loc) · 1.47 KB
/
sketch.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 app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
app.listen(8080);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
var users = [];
io.sockets.on('connection', function (socket) {
socket.on('set nickname', function (newname) {
socket.get('nickname', function(err, name){
users[ users.indexOf( name ) ] = newname;
socket.set('nickname', newname);
socket.broadcast.emit('nickname', {userid: name, name: newname});
});
});
socket.emit('userlist', users);
name = 'user_'+Math.floor(Math.random()*101);
users.push(name);
socket.set('nickname', name);
socket.emit('user', {userid: name});
io.sockets.emit('user connected', {userid: name});
socket.on('position', function(data){
socket.broadcast.emit('position', data);
});
socket.on('draw', function(data){
socket.broadcast.emit('draw', data);
});
socket.on('mousedown', function(data){
socket.broadcast.emit('mousedown', data);
});
socket.on('color', function(data){
socket.broadcast.emit('color', data);
});
socket.on('disconnect', function(){
socket.get('nickname', function(err, name){
socket.emit('close');
users.slice(users.indexOf( name ), 1);
io.sockets.emit('user disconnected', {userid: name});
});
});
});