UNPKG

commonui-lib-test

Version:

"#common ui lib test"

131 lines (130 loc) 4.23 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const lodash_es_1 = require("lodash-es"); const clientConfig_1 = require("../../configData/clientConfig"); const messageHandler_1 = require("../../external/message/messageHandler"); const DialogHandler_1 = require("./DialogHandler"); /** * @api {class} KeepAliveHandler * @apiName KeepAliveHandler * @apiGroup KeepAliveHandler * @apiDescription KeepAlive Handler */ class KeepAliveHandler { constructor(root) { this.pingTime = 60000; this.checkTime = 5000; this.maxLatency = 5000; this.gameRoot = root; } /** * @api {function} initHandler * @apiName initHandler * @apiGroup KeepAliveHandler * @apiDescription init keepalive handler */ initHandler() { this.pingTime = clientConfig_1.keepAliveConfig.pingTime; this.checkTime = clientConfig_1.keepAliveConfig.checkTime; this.maxLatency = clientConfig_1.keepAliveConfig.maxLatency; this.pingData = { ping: 0, reply: 0 }; this.subscriber = messageHandler_1.default.commMsgHandler.updateAction.heartBeatData.subscribe(this.onHeartBeatData.bind(this)); this.startPingServer(); } /** * @api {function} dispose * @apiName dispose * @apiGroup KeepAliveHandler * @apiDescription dispose keepalive handler */ dispose() { this.stopPingServer(); this.gameRoot = null; } /** * @api {function} startPingServer * @apiName startPingServer * @apiGroup KeepAliveHandler * @apiDescription startPingServer */ startPingServer() { this.pingData = { ping: lodash_es_1.now(), reply: 0 }; messageHandler_1.default.commMsgHandler.responseAction.DO_HEART_BEAT.send(this.pingData); Laya.timer.once(this.checkTime, this, this.checkPingData, [this.pingData]); } /** * @api {function} stopPingServer * @apiName stopPingServer * @apiGroup KeepAliveHandler * @apiDescription stopPingServer */ stopPingServer() { if (this.subscriber) this.subscriber.unsubscribe(); this.cleaAllTimer(); } /** * @api {function} onHeartBeatData * @apiName onHeartBeatData * @apiGroup KeepAliveHandler * @apiDescription on ping to server event */ onHeartBeatData(evt) { let { jsonCommand } = evt; let replyData = JSON.parse(jsonCommand); this.cleaAllTimer(); if (this.pingData.ping > replyData.ping) { this.onCheckPingDataFail(); } else { this.pingData.reply = lodash_es_1.now(); this.checkPingData(this.pingData); } } /** * @api {function} checkPingData * @apiName checkPingData * @apiGroup KeepAliveHandler * @apiDescription check ping to server data */ checkPingData(evt) { let { ping, reply } = evt; let latency = reply - ping; if (reply !== 0 && latency < this.maxLatency) { fgui.GRoot.inst.closeModalWait(); Laya.timer.once(this.pingTime, this, this.startPingServer); } else { this.onCheckPingDataFail(); } } /** * @api {function} onCheckPingDataFail * @apiName onCheckPingDataFail * @apiGroup KeepAliveHandler * @apiDescription on check ping to server fail event */ onCheckPingDataFail() { DialogHandler_1.default.Ins.showReconnect(1, null, this.backLobby, this); } /** * @api {function} cleaAllTimer * @apiName cleaAllTimer * @apiGroup KeepAliveHandler * @apiDescription clear all check timer */ cleaAllTimer() { Laya.timer.clear(this, this.startPingServer); Laya.timer.clear(this, this.checkPingData); } /** * @api {function} backLobby * @apiName backLobby * @apiGroup KeepAliveHandler * @apiDescription ping error window back button event */ backLobby() { this.gameRoot.destroy(); } } exports.default = KeepAliveHandler;