repositories
/
poolifier.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
build(deps-dev): bump @types/node
[poolifier.git]
/
examples
/
typescript
/
websocket-server-pool
/
ws-hybrid
/
src
/
websocket-server-worker.ts
diff --git
a/examples/typescript/websocket-server-pool/ws-hybrid/src/websocket-server-worker.ts
b/examples/typescript/websocket-server-pool/ws-hybrid/src/websocket-server-worker.ts
index 8b1bf2e83ca25dba3c590be6817c241e4e6f4ce4..ec6a3d18736f119cb27f5e32988fe464f5d2d3d4 100644
(file)
--- a/
examples/typescript/websocket-server-pool/ws-hybrid/src/websocket-server-worker.ts
+++ b/
examples/typescript/websocket-server-pool/ws-hybrid/src/websocket-server-worker.ts
@@
-15,33
+15,42
@@
import {
} from './types.js'
const emptyFunction = (): void => {
} from './types.js'
const emptyFunction = (): void => {
- /*
*
Intentional */
+ /* Intentional */
}
class WebSocketServerWorker extends ClusterWorker<
ClusterWorkerData,
ClusterWorkerResponse
> {
}
class WebSocketServerWorker extends ClusterWorker<
ClusterWorkerData,
ClusterWorkerResponse
> {
+ private static wss: WebSocketServer
+ private static requestHandlerPool: DynamicThreadPool<
+ ThreadWorkerData<DataPayload>,
+ ThreadWorkerResponse<DataPayload>
+ >
+
private static readonly startWebSocketServer = (
workerData?: ClusterWorkerData
): ClusterWorkerResponse => {
private static readonly startWebSocketServer = (
workerData?: ClusterWorkerData
): ClusterWorkerResponse => {
- const { port } = workerData as ClusterWorkerData
- const wss = new WebSocketServer({ port }, () => {
- console.info(
- `⚡️[ws server]: WebSocket server is started in cluster worker at ws://localhost:${port}/`
- )
- })
+ const { port, workerFile, minWorkers, maxWorkers, ...poolOptions } =
+ workerData as ClusterWorkerData
WebSocketServerWorker.requestHandlerPool = new DynamicThreadPool<
ThreadWorkerData<DataPayload>,
ThreadWorkerResponse<DataPayload>
>(
WebSocketServerWorker.requestHandlerPool = new DynamicThreadPool<
ThreadWorkerData<DataPayload>,
ThreadWorkerResponse<DataPayload>
>(
- workerData?.minWorkers ?? 1,
- workerData?.maxWorkers ?? availableParallelism(),
- workerData?.workerFile as string
+ minWorkers ?? 1,
+ maxWorkers ?? availableParallelism(),
+ workerFile,
+ poolOptions
)
)
- wss.on('connection', ws => {
+ WebSocketServerWorker.wss = new WebSocketServer({ port }, () => {
+ console.info(
+ `⚡️[ws server]: WebSocket server is started in cluster worker at ws://localhost:${port}/`
+ )
+ })
+
+ WebSocketServerWorker.wss.on('connection', ws => {
ws.on('error', console.error)
ws.on('message', (message: RawData) => {
const { type, data } = JSON.parse(
ws.on('error', console.error)
ws.on('message', (message: RawData) => {
const { type, data } = JSON.parse(
@@
-82,17
+91,17
@@
ClusterWorkerResponse
})
return {
status: true,
})
return {
status: true,
- port: wss.options.port
+ port:
WebSocketServerWorker.
wss.options.port
}
}
}
}
- private static requestHandlerPool: DynamicThreadPool<
- ThreadWorkerData<DataPayload>,
- ThreadWorkerResponse<DataPayload>
- >
-
public constructor () {
public constructor () {
- super(WebSocketServerWorker.startWebSocketServer)
+ super(WebSocketServerWorker.startWebSocketServer, {
+ killHandler: async () => {
+ await WebSocketServerWorker.requestHandlerPool.destroy()
+ WebSocketServerWorker.wss.close()
+ }
+ })
}
}
}
}