forked from bjorn/mana.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
66 lines (47 loc) · 1.5 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
var express = require('express');
var app = express.createServer();
var io = require('socket.io').listen(app);
var players = [];
var nextId = 0;
// Game server part
io.sockets.on('connection', function(socket) {
var player
socket.on('logon', function(pos) {
// Create the player
player = { id: nextId++, x: pos.x, y: pos.y };
// Send existing players to client
socket.emit('players', players);
// Send the new player to other clients
socket.broadcast.emit('connected', player)
// Add client to list of players
players.push(player);
})
socket.on('move', function(data) {
if (player) {
player.x = data.x;
player.y = data.y;
// Broadcast position change to all other clients
socket.broadcast.emit('moved', player)
}
});
socket.on('disconnect', function() {
players.splice(players.indexOf(player), 1);
io.sockets.emit('disconnected', player);
});
});
// HTTP server part
app.configure(function() {
app.get('/version', function(req, res) {
res.send('0.0.1');
});
app.use(express.logger());
app.use(express.static(__dirname));
});
app.configure('development', function() {
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function() {
app.use(express.errorHandler());
});
app.listen(80);
console.log('Mana.js server listening on port %s', app.address().port);