feat: websocket modules

This commit is contained in:
Roberto Tonino
2021-05-11 21:45:24 +02:00
parent 8c92f78e52
commit 62bdb71ede
5 changed files with 38 additions and 6 deletions

View File

@@ -0,0 +1,22 @@
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 => {
wsModules.forEach(module => {
ws.on(module.eventName, module.cb)
})
})
wss.on('error', () => {
consoleError('An error occurred to the WebSocket server.')
})
wss.on('close', () => {
consoleInfo('Connection to the WebSocket server closed.')
})
}

View File

@@ -0,0 +1,9 @@
import { consoleInfo } from '../../helpers/errors'
const eventName = 'message'
const cb = (message: string) => {
consoleInfo(`received: ${message}`)
}
export default { eventName, cb }

View File

@@ -0,0 +1,3 @@
import connection from './connection'
export default [connection]