-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
50 lines (39 loc) · 1.07 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
var http = require('http');
var express = require('express');
var socket_io = require('socket.io');
var app = express();
app.use(express.static('public'));
var server = http.Server(app);
var io = socket_io(server);
var users = [];
var userCount = 0;
io.on('connection', function(socket) {
userCount++;
var user = new Object();
user['id'] = socket.id;
if (userCount == 1) {
user['drawer'] = true;
user['guesser'] = false;
} else {
user['drawer'] = false;
user['guesser'] = true;
}
console.log(user);
users.push(user);
// send this only to the same client connection
socket.emit('user', user);
if (userCount == 2) {
io.emit('begin');
}
socket.on('draw', function(position) {
socket.broadcast.emit('draw', position);
});
socket.on('guess', function(guess) {
socket.broadcast.emit('guess', guess);
});
socket.on('disconnect', function(socket) {
userCount--;
users.splice(users.indexOf(user['id'] == socket.id), 1);
});
});
server.listen(8080);