diff --git a/public/Communication.js b/public/Communication.js index 05f46a3..77baff8 100644 --- a/public/Communication.js +++ b/public/Communication.js @@ -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, } diff --git a/public/game.js b/public/game.js index 87a89e4..c25c54d 100644 --- a/public/game.js +++ b/public/game.js @@ -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)