2021-05-13 09:40:32 +00:00
|
|
|
import WebSocket from 'ws'
|
2021-05-11 19:45:24 +00:00
|
|
|
import { Server as WsServer } from 'ws'
|
|
|
|
import { consoleError, consoleInfo } from '../helpers/errors'
|
|
|
|
import wsModules from './modules'
|
|
|
|
|
|
|
|
// ? Is this needed?
|
|
|
|
// ? https://github.com/websockets/ws#how-to-detect-and-close-broken-connections
|
|
|
|
|
|
|
|
export const registerWebsocket = (wss: WsServer) => {
|
|
|
|
wss.on('connection', ws => {
|
2021-05-13 09:40:32 +00:00
|
|
|
ws.on('message', (message)=>{
|
|
|
|
consoleInfo(`received: ${message}`)
|
|
|
|
const data = JSON.parse(message.toString())
|
|
|
|
wsModules.forEach(module => {
|
|
|
|
if (data.key === module.eventName) module.cb(data.data, ws, wss)
|
|
|
|
})
|
2021-05-11 19:45:24 +00:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
|
|
|
wss.on('error', () => {
|
|
|
|
consoleError('An error occurred to the WebSocket server.')
|
|
|
|
})
|
|
|
|
|
|
|
|
wss.on('close', () => {
|
|
|
|
consoleInfo('Connection to the WebSocket server closed.')
|
|
|
|
})
|
|
|
|
}
|