some fixes for players syncing

This commit is contained in:
Zutatensuppe 2020-12-03 21:11:52 +01:00
parent e75cd7406f
commit 3d1a9ca9c7
5 changed files with 54 additions and 46 deletions

View file

@ -50,43 +50,19 @@ class WebSocketServer {
return
}
socket.isAlive = true
socket.on('pong', function () {
this.isAlive = true;
})
socket.on('message', (data) => {
console.log(`ws| `, data)
console.log(`ws`, socket.protocol, data)
this.evt.dispatch('message', {socket, data})
})
})
this._interval = setInterval(() => {
this._websocketserver.clients.forEach((socket) => {
if (socket.isAlive === false) {
return socket.terminate()
}
socket.isAlive = false
this.evt.dispatch('close', {socket})
socket.ping(() => { })
})
}, 30000)
this._websocketserver.on('close', () => {
clearInterval(this._interval)
})
}
notifyOne(data, socket) {
if (socket.isAlive) {
socket.send(JSON.stringify(data))
}
}
notifyAll(data) {
this._websocketserver.clients.forEach((socket) => {
this.notifyOne(data, socket)
})
socket.send(JSON.stringify(data))
}
}