-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
180 lines (143 loc) · 4.78 KB
/
index.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
"use strict";
const http = require("http");
const WebSocket = require("ws");
const httpServerEngine = require("./httpServerEngine.js").httpServerEngine;
const EVENTS = {
ONLINECLIENTS: 0
, SCORE: 1
, NEWSQUARE: 2
, HIT: 3
, CLICK: 4
};
const PORT = process.env.PORT || 5000;
const PING_TIMEOUT = 10000;
const MAX_SQUARES = 5;
const MIN_WIDTH = 10; // px
const MIN_HEIGHT = 10; // px
const HttpServer = new http.createServer(httpServerEngine).listen(PORT);
const WebSocketServer = WebSocket.Server;
const wss = new WebSocketServer({ server: HttpServer });
wss.on('connection', (ws) => {
ws.id = (Math.random()).toString().substring(2);
ws.alive = true;
ws.score = 0;
console.log('Connected ws.id: ' + ws.id);
sendOnlineClients();
ws.on('pong', () => {
console.log('pong ws.id: ' + ws.id);
ws.alive = true;
});
ws.on('close', () => {
console.log('disconnected ws.id: ' + ws.id);
sendOnlineClients();
});
ws.onmessage = (event) => {
let message = new Uint16Array(event.data.length / Uint16Array.BYTES_PER_ELEMENT);
message = bufferToUint16Array(event.data);
messageRouter(ws, message);
};
for(let squareId in squares) {
ws.send(squares[squareId]);
}
ws.lastMessageTime = Date.now();
});
const bufferToUint16Array = (data) => {
let message = new Uint16Array(data.length / Uint16Array.BYTES_PER_ELEMENT);
let viewIndex = 0;
for (let bufferIndex = 0; bufferIndex < data.length; bufferIndex += message.BYTES_PER_ELEMENT) {
message[viewIndex] = data.readUInt16LE(bufferIndex);
viewIndex++;
}
return message;
};
const messageRouter = (ws, message) => {
//console.log(message);
switch (message[0]) {
case EVENTS.CLICK:
click(ws, message[1], message[2]);
break;
default:
console.log(`Unknown message: ${message}`);
}
};
let squares = {};
const sendNewSquare = () => {
if(Object.keys(squares).length >= MAX_SQUARES) {
return false;
}
let squareId = parseInt((Math.random()).toString().substring(2, 6), 10);
let square = new Uint16Array([
EVENTS.NEWSQUARE
, squareId
, parseInt(Math.random() * 100 * 5) // x
, parseInt(Math.random() * 100 * 5) // y
, parseInt(MIN_WIDTH + Math.random() * 100) // width
, parseInt(MIN_HEIGHT + Math.random() * 100) // height
]);
squares[squareId] = square;
wss.clients.forEach(ws => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(square);
ws.lastMessageTime = Date.now();
}
});
};
// Init squares
for(let i = 0; i <= MAX_SQUARES; i++) {
sendNewSquare();
}
const click = (ws, x, y) => {
console.log(`click: ${ws.id} (${x}, ${y})`);
let oldScore = ws.score;
for(let squareId in squares) {
//console.log(`${squares[squareId][0]} - ${squares[squareId][0]} ${squares[squareId][2]}`);
//console.log(`${squares[squareId][1]} - ${squares[squareId][0]} ${squares[squareId][3]}`);
if(x > squares[squareId][2]
&& x < squares[squareId][2] + squares[squareId][4]
&& y > squares[squareId][3]
&& y < squares[squareId][3] + squares[squareId][5]
) {
console.log(`hit ws.id: ${ws.id}`);
delete squares[squareId];
ws.score += 1;
let score = new Uint16Array([ EVENTS.SCORE, ws.score ]);
ws.send(score);
let hit = new Uint16Array([ EVENTS.HIT, squareId ]);
wss.clients.forEach(ws => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(hit);
ws.lastMessageTime = Date.now();
}
});
setInterval(sendNewSquare, 1000);
break;
}
}
if(oldScore === ws.score) {
console.log(`wuuuuuu ws.id: ${ws.id}`);
}
};
const sendOnlineClients = () => {
let onlineClients = new Uint16Array([ EVENTS.ONLINECLIENTS, wss.clients.size ]);
wss.clients.forEach(ws => {
if (ws.readyState === WebSocket.OPEN) {
ws.send(onlineClients);
ws.lastMessageTime = Date.now();
}
});
};
const pingClients = () => {
wss.clients.forEach(ws => {
if(ws.alive === false) {
ws.terminate();
return;
}
if(ws.lastMessageTime < (Date.now() - PING_TIMEOUT)) {
console.log("ping ws.id: " + ws.id);
ws.ping();
ws.alive = false;
ws.lastMessageTime = Date.now();
}
});
};
const pingInterval = setInterval(pingClients, 10000);