mirror of
https://gitlab.com/RemixDev/deemix-gui.git
synced 2024-12-29 11:06:13 +00:00
feat: websocket modules
This commit is contained in:
parent
8c92f78e52
commit
62bdb71ede
|
@ -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) => {
|
||||
|
|
|
@ -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 {
|
||||
|
|
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]
|
Loading…
Reference in a new issue