-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
70 lines (56 loc) · 1.79 KB
/
server.js
File metadata and controls
70 lines (56 loc) · 1.79 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
const WebSocket = require('ws');
const { WebSocketServer } = WebSocket;
const wss = new WebSocketServer({ port: 8080 });
// Store connected clients: id -> ws
const clients = new Map();
let nextId = 1;
console.log("Three64 LAN Server started on port 8080");
wss.on('connection', function connection(ws) {
const id = nextId++;
const color = Math.floor(Math.random() * 0xffffff);
console.log(`Client ${id} connected`);
// Notify new client of their ID
send(ws, { type: 'hello', id, color });
// Notify existing clients of new player
broadcast({ type: 'join', id, color }, ws);
// Send existing clients to new player
for (const [cid, cws] of clients) {
if (cws.readyState === WebSocket.OPEN && cws.userData) {
send(ws, { type: 'join', id: cid, color: cws.userData.color });
}
}
clients.set(id, ws);
ws.userData = { id, color };
ws.on('message', function message(data) {
try {
// Relay updates to all other clients
// We assume binary or JSON. If JSON string:
const msg = JSON.parse(data);
msg.id = id; // Ensure authoritative ID
if (msg.type === 'state' || msg.type === 'action' || msg.type === 'match') {
// Broadcast state/action/match to others
broadcast(msg, ws);
}
} catch (e) {
// If binary, just relay? For now assume JSON.
}
});
ws.on('close', function () {
console.log(`Client ${id} disconnected`);
clients.delete(id);
broadcast({ type: 'leave', id });
});
});
function send(ws, msg) {
if (ws.readyState === WebSocket.OPEN) {
ws.send(JSON.stringify(msg));
}
}
function broadcast(msg, excludeWs) {
const payload = JSON.stringify(msg);
for (const [id, ws] of clients) {
if (ws !== excludeWs && ws.readyState === WebSocket.OPEN) {
ws.send(payload);
}
}
}