-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathserver.js
More file actions
75 lines (71 loc) · 2.42 KB
/
server.js
File metadata and controls
75 lines (71 loc) · 2.42 KB
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
var server = function() {
var http = require('http'),
io = require('socket.io'),
fs = require('fs')
var urls = {
'/favicon.ico' : ['image/x-icon', fs.readFileSync('favicon.ico')],
'/client.js' : ['text/javascript', fs.readFileSync('client.js')],
'/processing.js' : ['text/javascript', fs.readFileSync('processing-1.0.0.js')],
'/' : ['text/html', fs.readFileSync('index.html')]
}
var server = http.createServer(function(req, res){
var data = urls[req.url] || ['text/html','Not Found'];
res.writeHead(200, {'Content-Type': data[0]});
res.write(data[1]);
res.end();
});
server.listen(3000,"127.0.0.1");
var clients = {};
var state = {};
// called when new client connects
var clientConnected = function(client) {
var newState = {
'sessionId' : client.sessionId,
'color' : [Math.random(), Math.random(), Math.random()],
'pos' : [10 * Math.random(), 10 * Math.random()]}
clients[client.sessionId] = client;
state[client.sessionId] = newState;
// new client gets full game state
client.send({'event':'welcome', 'who': client.sessionId, 'state':state});
for(c in clients) {
if(c != client.sessionId) {
// all other clients get only new client state
clients[c].send({'event':'enter', 'who': client.sessionId, 'state':newState});
}
}
}
// called when existing client disconnects
var clientDisconnected = function(client) {
delete clients[client.sessionId];
delete state[client.sessionId];
// notify all clients this one's gone
for(c in clients) {
clients[c].send({'event':'exit', 'who': client.sessionId});
}
}
// called when client data is recieved
var clientMessaged = function(client, data) {
state[client.sessionId]['pos'] = data['pos'];
// notify all clients of new data
for(c in clients) {
// send just the data that changed to everyone but the changer
if(c != client.sessionId) {
clients[c].send({'event':'move', 'who': data['who'], 'pos':data['pos']});
}
}
}
// socket.io server
var socket = io.listen(server);
socket.on('connection', function(client){
clientConnected(client);
client.on('message', function(data){
clientMessaged(client,data);
});
client.on('disconnect', function(){
clientDisconnected(client);
});
});
// return accessor into internals
var r = {};
return r;
}();