ClusterWorkerData,
ClusterWorkerResponse
> {
+ private static wss: WebSocketServer
+ private static requestHandlerPool: DynamicThreadPool<
+ ThreadWorkerData<DataPayload>,
+ ThreadWorkerResponse<DataPayload>
+ >
+
private static readonly startWebSocketServer = (
workerData?: ClusterWorkerData
): ClusterWorkerResponse => {
const { port } = workerData as ClusterWorkerData
- const wss = new WebSocketServer({ port }, () => {
+ WebSocketServerWorker.wss = new WebSocketServer({ port }, () => {
console.info(
`⚡️[ws server]: WebSocket server is started in cluster worker at ws://localhost:${port}/`
)
workerData?.workerFile as string
)
- wss.on('connection', ws => {
+ WebSocketServerWorker.wss.on('connection', (ws) => {
ws.on('error', console.error)
ws.on('message', (message: RawData) => {
const { type, data } = JSON.parse(
case MessageType.echo:
WebSocketServerWorker.requestHandlerPool
.execute({ data }, 'echo')
- .then(response => {
+ .then((response) => {
ws.send(
JSON.stringify({
type: MessageType.echo,
case MessageType.factorial:
WebSocketServerWorker.requestHandlerPool
.execute({ data }, 'factorial')
- .then(response => {
+ .then((response) => {
ws.send(
JSON.stringify({
type: MessageType.factorial,
})
return {
status: true,
- port: wss.options.port
+ port: WebSocketServerWorker.wss.options.port
}
}
- private static requestHandlerPool: DynamicThreadPool<
- ThreadWorkerData<DataPayload>,
- ThreadWorkerResponse<DataPayload>
- >
-
public constructor () {
- super(WebSocketServerWorker.startWebSocketServer)
+ super(WebSocketServerWorker.startWebSocketServer, {
+ killHandler: async () => {
+ WebSocketServerWorker.wss.close()
+ await WebSocketServerWorker.requestHandlerPool.destroy()
+ }
+ })
}
}