Added websocket server

This commit is contained in:
RemixDev 2021-04-18 12:11:51 +02:00
parent 1bf94ea65d
commit 0b810d7188
3 changed files with 505 additions and 551 deletions

View File

@ -14,7 +14,8 @@
"cookie-parser": "1.4.5", "cookie-parser": "1.4.5",
"debug": "2.6.9", "debug": "2.6.9",
"express": "4.17.1", "express": "4.17.1",
"morgan": "1.10.0" "morgan": "1.10.0",
"ws": "^7.4.5"
}, },
"devDependencies": { "devDependencies": {
"@nuxtjs/eslint-config": "6.0.0", "@nuxtjs/eslint-config": "6.0.0",
@ -23,6 +24,7 @@
"@types/express": "4.17.11", "@types/express": "4.17.11",
"@types/morgan": "1.9.2", "@types/morgan": "1.9.2",
"@types/node": "14.14.37", "@types/node": "14.14.37",
"@types/ws": "^7.4.1",
"@typescript-eslint/eslint-plugin": "4.21.0", "@typescript-eslint/eslint-plugin": "4.21.0",
"@typescript-eslint/parser": "4.21.0", "@typescript-eslint/parser": "4.21.0",
"eslint": "7.23.0", "eslint": "7.23.0",

View File

@ -1,5 +1,6 @@
import http from 'http' import http from 'http'
import express, { Application } from 'express' import express, { Application } from 'express'
import {Server as wsServer } from 'ws'
import initDebug from 'debug' import initDebug from 'debug'
import { registerMiddlewares } from './middlewares' import { registerMiddlewares } from './middlewares'
@ -15,6 +16,7 @@ const PORT = normalizePort(process.env.PORT || '6595')
const debug = initDebug('deemix-gui:server') const debug = initDebug('deemix-gui:server')
const app: Application = express() const app: Application = express()
const ws = new wsServer({ noServer: true })
const server = http.createServer(app) const server = http.createServer(app)
/* === Middlewares === */ /* === Middlewares === */
@ -34,5 +36,10 @@ app.set('port', PORT)
server.listen(PORT) server.listen(PORT)
/* === Server callbacks === */ /* === Server callbacks === */
server.on('upgrade', (request, socket, head) => {
ws.handleUpgrade(request, socket, head, socket => {
ws.emit('connection', socket, request)
})
})
server.on('error', getErrorCb(PORT)) server.on('error', getErrorCb(PORT))
server.on('listening', getListeningCb(server, debug)) server.on('listening', getListeningCb(server, debug))

File diff suppressed because it is too large Load Diff