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,
}

View file

@ -5,13 +5,15 @@ import Camera from './Camera.js'
import Graphics from './Graphics.js'
import Debug from './Debug.js'
import Communication from './Communication.js'
import Util from './../common/Util.js'
import Util, { logger } from './../common/Util.js'
import PuzzleGraphics from './PuzzleGraphics.js'
import Game from './../common/GameCommon.js'
import fireworksController from './Fireworks.js'
import Protocol from '../common/Protocol.js'
import Time from '../common/Time.js'
const log = logger('game.js')
export const MODE_PLAY = 'play'
export const MODE_REPLAY = 'replay'
@ -217,6 +219,11 @@ export async function main(gameId, clientId, wsAddress, MODE, TARGET_EL, HUD) {
gameStartTs: null,
}
Communication.onConnectionLost(() => {
log('connection lost ... should reload / hit reconnect button / etc.')
})
let TIME
if (MODE === MODE_PLAY) {
const game = await Communication.connect(wsAddress, gameId, clientId)