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

View file

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