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