-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
52 lines (36 loc) · 1.22 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
var http = require('http');
var static = require('node-static');
var files = new static.Server('./public');
var server = http.createServer(function(request, response) {
files.serve(request, response);
});
server.listen(8888);
var io = require('socket.io').listen(server, { log: false });
var occupancy = new Array();
io.sockets.on('connection', function(socket) {
console.log('client connected');
socket.emit('occupy', { seats: occupancy });
socket.on('occupy', function(data) {
if (occupancy.indexOf(data.seat) == -1) { // free
occupancy.push(data.seat);
socket.emit('occupy_success', data);
socket.broadcast.emit('occupy', { seats: [ data.seat ] });
console.log('Seat ' + data.seat + ' occupied');
}
});
socket.on('free', function(data) {
var idx = occupancy.indexOf(data.seat);
if (idx != -1) { // occupied
occupancy.splice(idx, 1);
socket.emit('free_success', data);
socket.broadcast.emit('free', { seats: [ data.seat ] });
console.log('Seat ' + data.seat + ' freed');
}
});
socket.on('hover', function(data) {
socket.broadcast.emit('hover', { seats: [ data.seat ] });
});
socket.on('unHover', function(data) {
socket.broadcast.emit('unHover', { seats: [ data.seat ] });
});
});