commonui-lib-test
Version:
"#common ui lib test"
131 lines (130 loc) • 4.27 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_es_1 = require("lodash-es");
const clientConfig_1 = require("../../configData/clientConfig");
const DialogHandler_1 = require("./DialogHandler");
const messageHandler_1 = require("../../external/message/messageHandler");
/**
* @api {class} CKeepAliveHandler
* @apiName CKeepAliveHandler
* @apiGroup CKeepAliveHandler
* @apiDescription KeepAlive Handler
*/
class CKeepAliveHandler {
constructor(root) {
this.pingTime = 60000;
this.checkTime = 5000;
this.maxLatency = 5000;
this.gameRoot = root;
}
/**
* @api {function} initHandler
* @apiName initHandler
* @apiGroup CKeepAliveHandler
* @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.MessageHandler.Ins.commMsgHandler.updateAction.heartBeatData.subscribe(this.onHeartBeatData.bind(this));
this.startPingServer();
}
/**
* @api {function} dispose
* @apiName dispose
* @apiGroup CKeepAliveHandler
* @apiDescription dispose keepalive handler
*/
dispose() {
this.stopPingServer();
this.gameRoot = null;
}
/**
* @api {function} startPingServer
* @apiName startPingServer
* @apiGroup CKeepAliveHandler
* @apiDescription startPingServer
*/
startPingServer() {
this.pingData = { ping: lodash_es_1.now(), reply: 0 };
messageHandler_1.MessageHandler.Ins.commMsgHandler.responseAction.DO_HEART_BEAT.send(this.pingData);
Laya.timer.once(this.checkTime, this, this.checkPingData, [this.pingData]);
}
/**
* @api {function} stopPingServer
* @apiName stopPingServer
* @apiGroup CKeepAliveHandler
* @apiDescription stopPingServer
*/
stopPingServer() {
if (this.subscriber)
this.subscriber.unsubscribe();
this.cleaAllTimer();
}
/**
* @api {function} onHeartBeatData
* @apiName onHeartBeatData
* @apiGroup CKeepAliveHandler
* @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 CKeepAliveHandler
* @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 CKeepAliveHandler
* @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 CKeepAliveHandler
* @apiDescription clear all check timer
*/
cleaAllTimer() {
Laya.timer.clear(this, this.startPingServer);
Laya.timer.clear(this, this.checkPingData);
}
/**
* @api {function} backLobby
* @apiName backLobby
* @apiGroup CKeepAliveHandler
* @apiDescription ping error window back button event
*/
backLobby() {
this.gameRoot.destroy();
}
}
exports.default = CKeepAliveHandler;