repositories
/
poolifier.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
docs: generate documentation
[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 90250c040ffaac022c6b2b7c0bfea9fd28e1f226..46745d9657cc9f700fc3f9ad79789b8323bf0715 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,7
+15,7
@@
import {
} from './types.js'
const emptyFunction = (): void => {
} from './types.js'
const emptyFunction = (): void => {
- /*
*
Intentional */
+ /* Intentional */
}
class WebSocketServerWorker extends ClusterWorker<
}
class WebSocketServerWorker extends ClusterWorker<
@@
-31,23
+31,26
@@
ClusterWorkerResponse
private static readonly startWebSocketServer = (
workerData?: ClusterWorkerData
): ClusterWorkerResponse => {
private static readonly startWebSocketServer = (
workerData?: ClusterWorkerData
): ClusterWorkerResponse => {
- const { port } = workerData as ClusterWorkerData
- WebSocketServerWorker.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
)
)
- WebSocketServerWorker.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(
@@
-58,28
+61,28
@@
ClusterWorkerResponse
case MessageType.echo:
WebSocketServerWorker.requestHandlerPool
.execute({ data }, 'echo')
case MessageType.echo:
WebSocketServerWorker.requestHandlerPool
.execute({ data }, 'echo')
- .then(
(response)
=> {
+ .then(
response
=> {
ws.send(
JSON.stringify({
type: MessageType.echo,
data: response.data
})
)
ws.send(
JSON.stringify({
type: MessageType.echo,
data: response.data
})
)
- return
null
+ return
undefined
})
.catch(emptyFunction)
break
case MessageType.factorial:
WebSocketServerWorker.requestHandlerPool
.execute({ data }, 'factorial')
})
.catch(emptyFunction)
break
case MessageType.factorial:
WebSocketServerWorker.requestHandlerPool
.execute({ data }, 'factorial')
- .then(
(response)
=> {
+ .then(
response
=> {
ws.send(
JSON.stringify({
type: MessageType.factorial,
data: response.data
})
)
ws.send(
JSON.stringify({
type: MessageType.factorial,
data: response.data
})
)
- return
null
+ return
undefined
})
.catch(emptyFunction)
break
})
.catch(emptyFunction)
break
@@
-95,8
+98,8
@@
ClusterWorkerResponse
public constructor () {
super(WebSocketServerWorker.startWebSocketServer, {
killHandler: async () => {
public constructor () {
super(WebSocketServerWorker.startWebSocketServer, {
killHandler: async () => {
- WebSocketServerWorker.wss.close()
await WebSocketServerWorker.requestHandlerPool.destroy()
await WebSocketServerWorker.requestHandlerPool.destroy()
+ WebSocketServerWorker.wss.close()
}
})
}
}
})
}