feat: websocket modules
This commit is contained in:
22
server/src/websocket/index.ts
Normal file
22
server/src/websocket/index.ts
Normal 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.')
|
||||
})
|
||||
}
|
||||
9
server/src/websocket/modules/connection.ts
Normal file
9
server/src/websocket/modules/connection.ts
Normal file
@@ -0,0 +1,9 @@
|
||||
import { consoleInfo } from '../../helpers/errors'
|
||||
|
||||
const eventName = 'message'
|
||||
|
||||
const cb = (message: string) => {
|
||||
consoleInfo(`received: ${message}`)
|
||||
}
|
||||
|
||||
export default { eventName, cb }
|
||||
3
server/src/websocket/modules/index.ts
Normal file
3
server/src/websocket/modules/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import connection from './connection'
|
||||
|
||||
export default [connection]
|
||||
Reference in New Issue
Block a user