replace socket.io usage with WebSockets

This commit is contained in:
Nick Krecklow
2020-05-05 16:49:01 -05:00
parent 2ee9c8b919
commit ca9e127e3e
10 changed files with 240 additions and 169 deletions

View File

@ -1,8 +1,8 @@
const http = require('http')
const WebSocket = require('ws')
const finalHttpHandler = require('finalhandler')
const serveStatic = require('serve-static')
const io = require('socket.io')
const logger = require('./logger')
@ -12,52 +12,65 @@ function getRemoteAddr (req) {
class Server {
constructor (clientSocketHandler) {
this._clientSocketHandler = clientSocketHandler
this._connectedSockets = 0
this.createHttpServer()
this.createWebSocketServer(clientSocketHandler)
}
this._http = http.createServer(this.handleHttpRequest)
createHttpServer () {
const distServeStatic = serveStatic('dist/')
const faviconsServeStatic = serveStatic('favicons/')
this._distServeStatic = serveStatic('dist/')
this._faviconsServeStatic = serveStatic('favicons/')
this._http = http.createServer((req, res) => {
logger.log('info', '%s requested: %s', getRemoteAddr(req), req.url)
// Attempt to handle req using distServeStatic, otherwise fail over to faviconServeStatic
// If faviconServeStatic fails, pass to finalHttpHandler to terminate
distServeStatic(req, res, () => {
faviconsServeStatic(req, res, finalHttpHandler(req, res))
})
})
}
createWebSocketServer (proxyClientSocketHandler) {
this._wss = new WebSocket.Server({
server: this._http
})
this._wss.on('connection', (client, req) => {
logger.log('info', '%s connected, total clients: %d', getRemoteAddr(req), this.getConnectedClients())
// Bind disconnect event for logging
client.on('close', () => {
logger.log('info', '%s disconnected, total clients: %d', getRemoteAddr(req), this.getConnectedClients())
})
// Pass client off to proxy handler
proxyClientSocketHandler(client)
})
}
listen (host, port) {
this._http.listen(port, host)
this._io = io.listen(this._http)
this._io.on('connect', this.handleClientSocket)
logger.log('info', 'Started on %s:%d', host, port)
}
broadcast (event, payload) {
this._io.sockets.emit(event, payload)
}
handleHttpRequest = (req, res) => {
logger.log('info', '%s requested: %s', getRemoteAddr(req), req.url)
// Attempt to handle req using distServeStatic, otherwise fail over to faviconServeStatic
// If faviconServeStatic fails, pass to finalHttpHandler to terminate
this._distServeStatic(req, res, () => {
this._faviconsServeStatic(req, res, finalHttpHandler(req, res))
broadcast (payload) {
this._wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
client.send(payload)
}
})
}
handleClientSocket = (client) => {
this._connectedSockets++
logger.log('info', '%s connected, total clients: %d', getRemoteAddr(client.request), this._connectedSockets)
// Bind disconnect event for logging
client.on('disconnect', () => {
this._connectedSockets--
logger.log('info', '%s disconnected, total clients: %d', getRemoteAddr(client.request), this._connectedSockets)
getConnectedClients () {
let count = 0
this._wss.clients.forEach(client => {
if (client.readyState === WebSocket.OPEN) {
count++
}
})
// Pass client off to proxy handler
this._clientSocketHandler(client)
return count
}
}