increase process priority instead of offloading to workers
This commit is contained in:
+5
-6
@@ -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))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+13
-2
@@ -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
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
import { parentPort } from 'node:worker_threads'
|
||||
|
||||
parentPort.on('message', (message) => {
|
||||
parentPort.postMessage(JSON.stringify(message))
|
||||
})
|
||||
Reference in New Issue
Block a user