forked from cubiq/socket-balls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
41 lines (32 loc) · 1.17 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
var sys = require('sys'),
http = require('http'),
io = require('socket.io'),
server = http.createServer(),
socket = io.listen(server, { transports: ['websocket']}),
players = {};
// Set up events
socket.on('connection', function (client) {
// Send to the new user the list of active players
client.send({ type: 'playerslist', list: players });
// Add the new user to the list of players
players[client.sessionId] = { x:0, y:0 }
// Broadcast the new user to all players
socket.broadcast({ type: 'new', id: client.sessionId }, [client.sessionId]);
client.on('message', function (message) {
if (message.type != 'position') {
return;
}
// Broadcast the new user position
players[message.id] = { x: message.x, y: message.y };
//socket.broadcast({ type: 'position', id: message.id, x: message.x, y: message.y }, [client.sessionId]);
client.send({ type: 'position', list: players });
});
client.on('disconnect', function () {
// Remove the user from the list of players
delete players[this.sessionId];
// Broadcast the logged out user's id
socket.broadcast({ type: 'leave', id: this.sessionId });
});
});
// Start listening
server.listen(8008);