From 4f8dcebcd16e56a7213fcd201d3d4802ab1a9389 Mon Sep 17 00:00:00 2001 From: Thayol Date: Wed, 22 Jan 2025 15:00:32 +0900 Subject: [PATCH] increase process priority instead of offloading to workers --- Dockerfile | 5 +++-- public/index.html | 2 +- src/game.js | 11 +++++------ src/index.js | 15 +++++++++++++-- src/worker/object-to-json.js | 5 ----- tools/fake-clients.js | 23 +++++++++++++++++++++++ 6 files changed, 45 insertions(+), 16 deletions(-) delete mode 100644 src/worker/object-to-json.js create mode 100644 tools/fake-clients.js diff --git a/Dockerfile b/Dockerfile index cca44a8..9628782 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,5 +2,6 @@ FROM node:current-alpine WORKDIR /app COPY package.json package-lock.json ./ RUN npm install -COPY . . -CMD ["node", "js/index.js"] +COPY public ./public +COPY src ./src +CMD ["node", "src/index.js"] diff --git a/public/index.html b/public/index.html index 12d3a6b..0346538 100644 --- a/public/index.html +++ b/public/index.html @@ -8,7 +8,7 @@ "three": "/three/build/three.module.js", "three/addons/": "/three/examples/jsm/", "@tweenjs/tween.js": "/@tweenjs/tween.js/dist/tween.esm.js", - "stats.js": "/stats.js/src/stats.js" + "stats.js": "/stats.js/src/Stats.js" } } diff --git a/src/game.js b/src/game.js index 23dfe34..9fbbfa5 100644 --- a/src/game.js +++ b/src/game.js @@ -5,7 +5,6 @@ import Buff from './buff.js' import Entity from './entity.js' import Projectile from './projectile.js' import Terrain from './terrain.js' -import { Worker } from 'node:worker_threads' export default class Game { id = crypto.randomUUID() @@ -32,6 +31,10 @@ export default class Game { get tickBudget() { return this.#tickBudget } set logic(value) { this.#logic = value } + constructor() { + this.#eventEmitter.setMaxListeners(20) + } + action(id, options) { const entity = this.entities.find((it) => it.id == id) if (entity == null) { @@ -122,10 +125,6 @@ export default class Game { } subscription(websocket, id) { - const worker = new Worker('./src/worker/object-to-json.js') - const sendToWebSocket = function sendToWebSocket(message) { websocket.send(message) } // TODO: latency because workers wait for the main thread's next tick which is ~33ms - worker.on('message', sendToWebSocket) - return function builtSubscription() { const game = this @@ -136,7 +135,7 @@ export default class Game { const state = game.visionByTeam(team) state.currentTick = game.currentTick - worker.postMessage(state) + websocket.send(JSON.stringify(state)) } } diff --git a/src/index.js b/src/index.js index 6d999fe..aac1d5b 100644 --- a/src/index.js +++ b/src/index.js @@ -1,7 +1,16 @@ +import { Dungeon, Ravine } from './level.js' import { WebSocketExpress } from 'websocket-express' import express from 'express' import Game from './game.js' -import { Dungeon, Ravine } from './level.js' +import os from 'node:os' + +try { + // WARNING: process.nice can undermine dependencies? + os.setPriority(process.pid, os.constants.priority.PRIORITY_HIGHEST) +} +catch (error) { + console.warn('Could not adjust process priority on startup.') +} const app = new WebSocketExpress() const port = 1280 @@ -23,11 +32,13 @@ app.ws('/ws', async (req, res) => { const message = JSON.parse(rawData) console.log(message) if (message.action == 'join') { + const id = message.id websocket.send(JSON.stringify(game.joinReport())) - const subscription = game.subscription(websocket, message.id).bind(game) + const subscription = game.subscription(websocket, id).bind(game) game.eventEmitter.on('tick', subscription) websocket.on('close', () => { + console.log({ event: 'disconnected', id }) game.eventEmitter.removeListener('tick', subscription) }) return diff --git a/src/worker/object-to-json.js b/src/worker/object-to-json.js deleted file mode 100644 index 4a4d3ce..0000000 --- a/src/worker/object-to-json.js +++ /dev/null @@ -1,5 +0,0 @@ -import { parentPort } from 'node:worker_threads' - -parentPort.on('message', (message) => { - parentPort.postMessage(JSON.stringify(message)) -}) diff --git a/tools/fake-clients.js b/tools/fake-clients.js new file mode 100644 index 0000000..9e1c248 --- /dev/null +++ b/tools/fake-clients.js @@ -0,0 +1,23 @@ +import WebSocket from 'ws' + +const numberOfClients = 10 +const url = 'ws://localhost:1280/ws' + +for (let i = 1; i <= numberOfClients; i++) { + const id = `${i}` + const websocket = new WebSocket(url) + + websocket.onerror = () => websocket.close() + websocket.onopen = () => { + websocket.send(JSON.stringify({ action: 'join', id })) + console.log({ client: id, event: 'joined' }) + } + websocket.onclose = () => { + console.log({ client: id, event: 'disconnected' }) + } + + websocket.onmessage = (event) => { + const byteSize = new Blob([event.data]).size + // console.log({ client: id, received: `${byteSize} B of data` }) + } +}