-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathasteroids-server.js
More file actions
61 lines (61 loc) · 1.75 KB
/
asteroids-server.js
File metadata and controls
61 lines (61 loc) · 1.75 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
(function() {
var allTogetherNow, expire, http, server, sites;
var __hasProp = Object.prototype.hasOwnProperty, __bind = function(func, context) {
return function(){ return func.apply(context, arguments); };
};
http = require('http');
sites = {};
expire = function(site) {
var _i, _ref, client, now;
now = new Date().getTime();
_ref = site;
for (client in _ref) {
if (!__hasProp.call(_ref, client)) continue;
_i = _ref[client];
if (now - site[client].ts > 3000) {
delete site[client];
}
}
return setTimeout(expire, 1000, site);
};
allTogetherNow = false;
server = http.createServer(__bind(function(req, res) {
var _i, _ref, _result, client, m, parsed, r, site, t, url, x, y;
m = req.url.match(/x=([0-9\-\.]+)&y=([0-9\-\.]+)&angle=([0-9\-\.]+)/);
if (!(m)) {
return null;
}
_ref = m;
url = _ref[0];
x = _ref[1];
y = _ref[2];
t = _ref[3];
site = allTogetherNow || req.headers.referer;
if (!(typeof (_ref = sites[site]) !== "undefined" && _ref !== null)) {
sites[site] = {};
expire(sites[site]);
}
sites[site][req.client.remoteAddress + req.client.remotePort.toString()] = {
x: x,
y: y,
t: t,
ts: (new Date().getTime())
};
res.writeHead(200, {
'Content-Type': 'text/javascript'
});
r = (function() {
_result = []; _ref = sites[site];
for (client in _ref) {
if (!__hasProp.call(_ref, client)) continue;
_i = _ref[client];
_result.push(JSON.stringify(sites[site][client]));
}
return _result;
})();
parsed = ("OTHERS = [" + (r) + "];");
return res.end(parsed);
}, this));
server.listen(9200);
console.log("listening...");
}).call(this);