add msg when connection is lost

This commit is contained in:
Zutatensuppe 2021-05-15 17:40:04 +02:00
parent d5e0c89bf6
commit 9f7ac8d111
2 changed files with 18 additions and 2 deletions

View file

@ -6,10 +6,15 @@ import Protocol from './../common/Protocol.js'
/** @type WsClient */
let conn
let changesCallback = () => {}
let connectionLostCallback = () => {}
// TODO: change these to something like on(EVT, cb)
function onServerChange(callback) {
changesCallback = callback
}
function onConnectionLost(callback) {
connectionLostCallback = callback
}
function send(message) {
conn.send(JSON.stringify(message))
@ -42,6 +47,9 @@ function connect(address, gameId, clientId) {
throw `[ 2021-05-09 invalid connect msgType ${msgType} ]`
}
})
conn.onclose(() => {
connectionLostCallback()
})
send([Protocol.EV_CLIENT_INIT])
})
}
@ -87,6 +95,7 @@ export default {
connect,
connectReplay,
disconnect,
onServerChange,
sendClientEvent,
onServerChange,
onConnectionLost,
}