feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
/* WebSocket endpoint /ws. Every socket must say { t:'hello', v, token } within HELLO_MS, then its messages
|
|
|
|
|
are validated (RC.Proto.check), rate limited and handed to the lobby. Keepalive via ws ping/pong. */
|
|
|
|
|
'use strict';
|
|
|
|
|
const { WebSocketServer } = require('ws');
|
|
|
|
|
const RC = require('../../game/protocol.js');
|
|
|
|
|
const auth = require('../auth');
|
|
|
|
|
|
|
|
|
|
const HELLO_MS = 5000;
|
|
|
|
|
const RATE = 80; // messages per second per socket (inputs ~30/s + lobby)
|
2026-11-19 14:25:46 +01:00
|
|
|
const PING_MS = 2000; // ws ping every 2 s: keepalive + ping measurement
|
|
|
|
|
const DEAD_MS = 15000; // no pong for this long -> connection is dead
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
|
|
|
|
|
function attach(server, { lobby }) {
|
|
|
|
|
const wss = new WebSocketServer({ server, path: '/ws', maxPayload: RC.Proto.MAX_MSG });
|
|
|
|
|
|
|
|
|
|
wss.on('connection', ws => {
|
|
|
|
|
let user = null, tokens = RATE, last = Date.now();
|
2026-11-19 14:25:46 +01:00
|
|
|
ws.lastPong = Date.now();
|
|
|
|
|
ws.on('pong', () => {
|
|
|
|
|
ws.lastPong = Date.now();
|
|
|
|
|
if (ws.pingSent && user) lobby.setRtt(user.uid, Number(process.hrtime.bigint() - ws.pingSent) / 1e6);
|
|
|
|
|
ws.pingSent = 0n;
|
|
|
|
|
});
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
const send = msg => { if (ws.readyState === ws.OPEN) ws.send(JSON.stringify(msg)); };
|
|
|
|
|
const bye = (code, reason) => { try { ws.close(code, reason); } catch (e) { /* closed */ } };
|
|
|
|
|
const helloTimer = setTimeout(() => { if (!user) bye(4001, 'HELLO_TIMEOUT'); }, HELLO_MS);
|
|
|
|
|
|
|
|
|
|
ws.on('message', (data, isBinary) => {
|
|
|
|
|
// token bucket
|
|
|
|
|
const now = Date.now(); tokens = Math.min(RATE, tokens + (now - last) * RATE / 1000); last = now;
|
|
|
|
|
if (--tokens < 0) return bye(4008, 'RATE_LIMIT');
|
|
|
|
|
let msg;
|
|
|
|
|
try { msg = JSON.parse(isBinary ? data : data.toString()); } catch (e) { return send({ t: 'error', code: 'BAD_JSON' }); }
|
|
|
|
|
const c = RC.Proto.check(msg);
|
|
|
|
|
if (!c.ok) return send({ t: 'error', code: c.error });
|
|
|
|
|
if (!user) {
|
|
|
|
|
if (msg.t !== 'hello') return bye(4001, 'HELLO_FIRST');
|
|
|
|
|
if (msg.v !== RC.Proto.VERSION) { send({ t: 'error', code: 'OUTDATED', need: RC.Proto.VERSION }); return bye(4002, 'OUTDATED'); }
|
|
|
|
|
const u = auth.verify(msg.token);
|
|
|
|
|
if (!u) { send({ t: 'error', code: 'UNAUTH' }); return bye(4003, 'UNAUTH'); }
|
|
|
|
|
clearTimeout(helloTimer);
|
|
|
|
|
user = u;
|
|
|
|
|
send({ t: 'welcome', uid: u.uid, name: u.name, serverTime: Date.now(), v: RC.Proto.VERSION });
|
2026-11-19 10:02:51 +01:00
|
|
|
try { lobby.join({ uid: u.uid, name: u.name, admin: u.admin, send }); } catch (e) { console.error('[ws] join failed', e); bye(1011, 'SERVER_ERROR'); }
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (msg.t === 'hello') return;
|
2026-11-19 10:02:51 +01:00
|
|
|
try { lobby.handle(user.uid, msg); } catch (e) { console.error('[ws] handler failed for', msg.t, e); send({ t: 'error', code: 'SERVER_ERROR' }); }
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
});
|
|
|
|
|
|
2026-11-19 10:02:51 +01:00
|
|
|
ws.on('close', () => { clearTimeout(helloTimer); if (user) { try { lobby.leave(user.uid, send); } catch (e) { console.error('[ws] leave failed', e); } } });
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
ws.on('error', () => {});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const beat = setInterval(() => {
|
2026-11-19 14:25:46 +01:00
|
|
|
for (const ws of wss.clients) {
|
|
|
|
|
if (Date.now() - ws.lastPong > DEAD_MS) { ws.terminate(); continue; }
|
|
|
|
|
try { ws.pingSent = process.hrtime.bigint(); ws.ping(); } catch (e) { /* closed */ }
|
|
|
|
|
}
|
feat(server)!: websocket lobby, server-run matches, server economy, new schema (P1-2, P1-4, P2-1, P2-1b, P2-5)
Replaces the Firebase-style /db facade (rtdb, rules, nodes tree) with explicit REST endpoints and a
WebSocket hub. Login by name or optional e-mail, seed accounts via npm run db:reset.
BREAKING CHANGE: old clients (lobby/netfight over /db) no longer work against this server; database is new.
2026-11-19 01:52:30 +01:00
|
|
|
}, PING_MS);
|
|
|
|
|
if (beat.unref) beat.unref();
|
|
|
|
|
wss.on('close', () => clearInterval(beat));
|
|
|
|
|
return wss;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = { attach };
|