-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.js
86 lines (66 loc) · 1.97 KB
/
app.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
var sys = require('sys');
var ws = require('websocket-server');
var http = require('http');
var url = require('url');
var path = require('path');
var fs = require('fs');
http.createServer(function(request, response) {
var uri = url.parse(request.url).pathname;
var filename = path.join(process.cwd(), 'client/' + uri);
path.exists(filename, function(exists) {
if(!exists) {
response.writeHead(404, {"Content-Type": "text/plain"});
response.write("404 Not Found\n");
response.end();
return;
}
fs.readFile(filename, "binary", function(err, file) {
if(err) {
response.writeHead(500, {"Content-Type": "text/plain"});
response.write(err + "\n");
response.end();
return;
}
response.writeHead(200);
response.write(file, "binary");
response.end();
});
});
}).listen(8090);
console.log('HTTP server running at port 8090');
var clients = new Array();
var online = 0;
var server = ws.createServer();
server.addListener('connection', function (connection) {
var self = this;
var withBall = 0;
online += 1;
clients.push(connection.id);
this.broadcast(JSON.stringify({'type' : 'online', 'data' : online}));
connection.addListener('message', function(data) {
var data = JSON.parse(data);
var values = data.data;
var index = clients.indexOf(connection.id);
if (values[0] == 0) {
if (clients[index + 1]) {
withBall = index + 1;
} else {
withBall = 0;
}
} else {
if (clients[index - 1]) {
withBall = index - 1;
} else {
withBall = clients.length - 1;
}
}
self.send(clients[withBall], JSON.stringify({'type' : 'ball.get', 'data' : data.data}));
});
connection.addListener('close', function () {
clients.splice(clients.indexOf(connection.id), 1);
connection.close();
self.broadcast(JSON.stringify({'type' : 'online', 'data' : clients.length}));
});
});
server.listen(8190);
console.log('Websocket running at port 8190');