remove sockets when closed

This commit is contained in:
Zutatensuppe 2020-12-24 15:04:58 +01:00
parent 11e5c08154
commit 1c9fc428b9
2 changed files with 9 additions and 10 deletions

View file

@ -31,7 +31,6 @@ class WebSocketServer {
constructor(config) { constructor(config) {
this.config = config this.config = config
this._websocketserver = null this._websocketserver = null
this._interval = null
this.evt = new EvtBus() this.evt = new EvtBus()
} }
@ -49,15 +48,13 @@ class WebSocketServer {
socket.close() socket.close()
return return
} }
socket.on('message', (data) => { socket.on('message', (data) => {
console.log(`ws`, socket.protocol, data) console.log(`ws`, socket.protocol, data)
this.evt.dispatch('message', {socket, data}) this.evt.dispatch('message', {socket, data})
}) })
socket.on('close', () => {
this.evt.dispatch('close', {socket})
}) })
this._websocketserver.on('close', () => {
clearInterval(this._interval)
}) })
} }

View file

@ -121,11 +121,13 @@ const notify = (data, sockets) => {
} }
wss.on('close', async ({socket}) => { wss.on('close', async ({socket}) => {
try {
const proto = socket.protocol.split('|') const proto = socket.protocol.split('|')
const clientId = proto[0] const clientId = proto[0]
const gameId = proto[1] const gameId = proto[1]
if (Game.exists(gameId)) {
GameSockets.removeSocket(gameId, socket) GameSockets.removeSocket(gameId, socket)
} catch (e) {
console.error(e)
} }
}) })