From 62bdb71ede7f50752b54fe1169688d49ff63ff19 Mon Sep 17 00:00:00 2001 From: Roberto Tonino Date: Tue, 11 May 2021 21:45:24 +0200 Subject: [PATCH] feat: websocket modules --- server/src/app.ts | 9 +++------ server/src/helpers/errors.ts | 1 + server/src/websocket/index.ts | 22 ++++++++++++++++++++++ server/src/websocket/modules/connection.ts | 9 +++++++++ server/src/websocket/modules/index.ts | 3 +++ 5 files changed, 38 insertions(+), 6 deletions(-) create mode 100644 server/src/websocket/index.ts create mode 100644 server/src/websocket/modules/connection.ts create mode 100644 server/src/websocket/modules/index.ts diff --git a/server/src/app.ts b/server/src/app.ts index 3c82699..c69f93a 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -10,12 +10,13 @@ import indexRouter from './routes' import { normalizePort } from './helpers/port' import { getErrorCb, getListeningCb } from './helpers/server-callbacks' import { registerApis } from './routes/api/register' +import { registerWebsocket } from './websocket' const PORT = normalizePort(process.env.PORT || '6595') const debug = initDebug('deemix-gui:server') export const app: Application = express() -const wss = new WsServer({ noServer: true }) +export const wss = new WsServer({ noServer: true }) const server = http.createServer(app) /* === Middlewares === */ @@ -35,11 +36,7 @@ if (process.env.NODE_ENV !== 'test') { server.listen(PORT) } -wss.on('connection', ws => { - ws.on('message', message => { - console.log('received: %s', message) - }) -}) +registerWebsocket(wss) /* === Server callbacks === */ server.on('upgrade', (request, socket, head) => { diff --git a/server/src/helpers/errors.ts b/server/src/helpers/errors.ts index 14793e2..39ab13d 100644 --- a/server/src/helpers/errors.ts +++ b/server/src/helpers/errors.ts @@ -2,6 +2,7 @@ import { concat } from 'ramda' const prependDeemix = concat('[deemix-server]: ') +export const consoleInfo = (errorText: string) => console.info(prependDeemix(errorText)) export const consoleError = (errorText: string) => console.error(prependDeemix(errorText)) export class BadRequestError extends Error { diff --git a/server/src/websocket/index.ts b/server/src/websocket/index.ts new file mode 100644 index 0000000..ea7638c --- /dev/null +++ b/server/src/websocket/index.ts @@ -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.') + }) +} diff --git a/server/src/websocket/modules/connection.ts b/server/src/websocket/modules/connection.ts new file mode 100644 index 0000000..1e05a57 --- /dev/null +++ b/server/src/websocket/modules/connection.ts @@ -0,0 +1,9 @@ +import { consoleInfo } from '../../helpers/errors' + +const eventName = 'message' + +const cb = (message: string) => { + consoleInfo(`received: ${message}`) +} + +export default { eventName, cb } diff --git a/server/src/websocket/modules/index.ts b/server/src/websocket/modules/index.ts new file mode 100644 index 0000000..cee690d --- /dev/null +++ b/server/src/websocket/modules/index.ts @@ -0,0 +1,3 @@ +import connection from './connection' + +export default [connection]