Commit | Line | Data |
---|---|---|
8ff2efc7 JB |
1 | import { |
2 | ClusterWorker, | |
3 | DynamicThreadPool, | |
4 | availableParallelism | |
5 | } from 'poolifier' | |
02999424 JB |
6 | import { type RawData, WebSocketServer } from 'ws' |
7 | import { | |
8 | type ClusterWorkerData, | |
9 | type ClusterWorkerResponse, | |
10 | type DataPayload, | |
11 | type MessagePayload, | |
8ff2efc7 JB |
12 | MessageType, |
13 | type ThreadWorkerData, | |
14 | type ThreadWorkerResponse | |
02999424 | 15 | } from './types.js' |
02999424 JB |
16 | |
17 | const emptyFunction = (): void => { | |
18 | /** Intentional */ | |
19 | } | |
20 | ||
02999424 JB |
21 | class WebSocketServerWorker extends ClusterWorker< |
22 | ClusterWorkerData, | |
23 | ClusterWorkerResponse | |
24 | > { | |
3d49c6d2 JB |
25 | private static wss: WebSocketServer |
26 | private static requestHandlerPool: DynamicThreadPool< | |
27 | ThreadWorkerData<DataPayload>, | |
28 | ThreadWorkerResponse<DataPayload> | |
29 | > | |
30 | ||
8ff2efc7 JB |
31 | private static readonly startWebSocketServer = ( |
32 | workerData?: ClusterWorkerData | |
33 | ): ClusterWorkerResponse => { | |
34 | const { port } = workerData as ClusterWorkerData | |
3d49c6d2 | 35 | WebSocketServerWorker.wss = new WebSocketServer({ port }, () => { |
8ff2efc7 | 36 | console.info( |
80ccdab6 | 37 | `⚡️[ws server]: WebSocket server is started in cluster worker at ws://localhost:${port}/` |
8ff2efc7 JB |
38 | ) |
39 | }) | |
40 | ||
63ff88a9 JB |
41 | WebSocketServerWorker.requestHandlerPool = new DynamicThreadPool< |
42 | ThreadWorkerData<DataPayload>, | |
43 | ThreadWorkerResponse<DataPayload> | |
44 | >( | |
45 | workerData?.minWorkers ?? 1, | |
46 | workerData?.maxWorkers ?? availableParallelism(), | |
47 | workerData?.workerFile as string | |
48 | ) | |
49 | ||
8ebe6c30 | 50 | WebSocketServerWorker.wss.on('connection', (ws) => { |
8ff2efc7 JB |
51 | ws.on('error', console.error) |
52 | ws.on('message', (message: RawData) => { | |
53 | const { type, data } = JSON.parse( | |
54 | // eslint-disable-next-line @typescript-eslint/no-base-to-string | |
55 | message.toString() | |
56 | ) as MessagePayload<DataPayload> | |
57 | switch (type) { | |
58 | case MessageType.echo: | |
63ff88a9 | 59 | WebSocketServerWorker.requestHandlerPool |
8ff2efc7 | 60 | .execute({ data }, 'echo') |
8ebe6c30 | 61 | .then((response) => { |
8ff2efc7 JB |
62 | ws.send( |
63 | JSON.stringify({ | |
64 | type: MessageType.echo, | |
65 | data: response.data | |
66 | }) | |
67 | ) | |
68 | return null | |
69 | }) | |
70 | .catch(emptyFunction) | |
71 | break | |
72 | case MessageType.factorial: | |
63ff88a9 | 73 | WebSocketServerWorker.requestHandlerPool |
8ff2efc7 | 74 | .execute({ data }, 'factorial') |
8ebe6c30 | 75 | .then((response) => { |
8ff2efc7 JB |
76 | ws.send( |
77 | JSON.stringify({ | |
78 | type: MessageType.factorial, | |
79 | data: response.data | |
80 | }) | |
81 | ) | |
82 | return null | |
83 | }) | |
84 | .catch(emptyFunction) | |
85 | break | |
86 | } | |
87 | }) | |
88 | }) | |
89 | return { | |
90 | status: true, | |
3d49c6d2 | 91 | port: WebSocketServerWorker.wss.options.port |
8ff2efc7 JB |
92 | } |
93 | } | |
94 | ||
02999424 | 95 | public constructor () { |
8ff2efc7 | 96 | super(WebSocketServerWorker.startWebSocketServer) |
02999424 JB |
97 | } |
98 | } | |
99 | ||
100 | export const webSocketServerWorker = new WebSocketServerWorker() |