mirror of
https://github.com/Cameri/nostream.git
synced 2025-04-08 11:58:03 +02:00
fix: broadcasting received events to all workers
This commit is contained in:
parent
13bd023a1b
commit
0dc7e8352e
@ -60,6 +60,10 @@ export class WebSocketAdapter extends EventEmitter implements IWebSocketAdapter
|
||||
|
||||
public onBroadcast(event: Event): void {
|
||||
this.webSocketServer.emit(WebSocketServerAdapterEvent.Broadcast, event)
|
||||
process.send({
|
||||
eventName: WebSocketServerAdapterEvent.Broadcast,
|
||||
event,
|
||||
})
|
||||
}
|
||||
|
||||
public onSendEvent(event: Event): void {
|
||||
|
14
src/index.ts
14
src/index.ts
@ -45,6 +45,16 @@ if (cluster.isPrimary) {
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
cluster.on('message', (source, message) => {
|
||||
for (const worker of Object.values(cluster.workers)) {
|
||||
if (source.id === worker.id) {
|
||||
continue
|
||||
}
|
||||
|
||||
worker.send(message)
|
||||
}
|
||||
})
|
||||
|
||||
process.on('SIGINT', exitHandler)
|
||||
process.on('uncaughtException', exitHandler)
|
||||
} else if (cluster.isWorker) {
|
||||
@ -76,5 +86,9 @@ if (cluster.isPrimary) {
|
||||
process.on('SIGINT', exitHandler)
|
||||
process.on('uncaughtException', exitHandler)
|
||||
|
||||
process.on('message', (message: { eventName: string, event: unknown }) => {
|
||||
adapter.emit(message.eventName, message.event)
|
||||
})
|
||||
|
||||
console.log(`worker ${process.pid} - listening on port ${port}`)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user