+
+ WebSocketServerWorker.wss.on('connection', ws => {
+ ws.on('error', console.error)
+ ws.on('message', (message: RawData) => {
+ const { type, data } = JSON.parse(
+ // eslint-disable-next-line @typescript-eslint/no-base-to-string
+ message.toString()
+ ) as MessagePayload<DataPayload>
+ switch (type) {
+ case MessageType.echo:
+ WebSocketServerWorker.requestHandlerPool
+ .execute({ data }, 'echo')
+ .then(response => {
+ ws.send(
+ JSON.stringify({
+ type: MessageType.echo,
+ data: response.data
+ })
+ )
+ return undefined
+ })
+ .catch(emptyFunction)
+ break
+ case MessageType.factorial:
+ WebSocketServerWorker.requestHandlerPool
+ .execute({ data }, 'factorial')
+ .then(response => {
+ ws.send(
+ JSON.stringify(
+ {
+ type: MessageType.factorial,
+ data: response.data
+ },
+ (_, v) => (typeof v === 'bigint' ? v.toString() : v)
+ )
+ )
+ return undefined
+ })
+ .catch(emptyFunction)
+ break
+ }
+ })
+ })
+ return {
+ status: true,
+ port: WebSocketServerWorker.wss.options.port
+ }