-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathserver.js
109 lines (79 loc) · 2.66 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
var express = require('express')
, app = express(app)
, server = require('http').createServer(app);
// serve static files from the current directory
app.use(express.static(__dirname));
//we'll keep clients data here
var clients = {};
//get EurecaServer class
var EurecaServer = require('eureca.io').EurecaServer;
//create an instance of EurecaServer
var eurecaServer = new EurecaServer({allow:['setId', 'spawnAnotherPlayer', 'kill', 'updateState']});
//attach eureca.io to our http server
eurecaServer.attach(server);
//eureca.io provides events to detect clients connect/disconnect
//detect client connection
eurecaServer.onConnect(function (conn) {
console.log('New Client id=%s ', conn.id, conn.remoteAddress);
//the getClient method provide a proxy allowing us to call remote client functions
var remote = eurecaServer.getClient(conn.id);
//register the client
clients[conn.id] = {id:conn.id, remote:remote, ip: conn.remoteAddress}
//here we call setId (defined in the client side)
remote.setId(conn.id);
});
//detect client disconnection
eurecaServer.onDisconnect(function (conn) {
console.log('Client disconnected ', conn.id);
var removeId = clients[conn.id].id;
delete clients[conn.id];
for (var c in clients)
{
var remote = clients[c].remote;
//here we call kill() method defined in the client side
remote.kill(conn.id);
}
});
eurecaServer.exports.handshake = function()
{
for (var c in clients)
{
var remote = clients[c].remote;
for (var cc in clients)
{
//send latest known position
var x = clients[cc].laststate ? clients[cc].laststate.x: 0;
var y = clients[cc].laststate ? clients[cc].laststate.y: 0;
//remote.spawnAnotherPlayer(clients[cc].id, x, y);
}
}
}
eurecaServer.exports.spawnOtherPlayers = function()
{
for (var c in clients)
{
var remote = clients[c].remote;
for (var cc in clients)
{
//send latest known position
var x = clients[cc].laststate ? clients[cc].laststate.x: 0;
var y = clients[cc].laststate ? clients[cc].laststate.y: 0;
var color = clients[cc].laststate ? clients[cc].laststate.playerColor: null;
remote.spawnAnotherPlayer(clients[cc].id, x, y, color, clients[cc].ip.ip);
}
}
}
//be exposed to client side
eurecaServer.exports.handleKeys = function (keys) {
var conn = this.connection;
var updatedClient = clients[conn.id];
for (var c in clients)
{
var remote = clients[c].remote;
//console.log('updating '+ c +' with ' + keys);
remote.updateState(updatedClient.id, keys);
//keep last known state so we can send it to new connected clients
clients[c].laststate = keys;
}
}
server.listen(process.env.PORT || 8000);