loadScript(["./js/communication/requestHandler.js"]);
var REQUESTS = {};
var TIMESTAMP_NONE = -1;
function requestToString(code, data, timestamp) {
var s = String.fromCharCode(code + 'A'.charCodeAt(0));
s += '#';
if (!isDefined(timestamp)) {
timestamp = TIMESTAMP_NONE;
}
s += timestamp;
s += '#';
s += JSON.stringify(data);
return s;
}
var __connError__ = false;
function connectionInit(event) {
console.log("Socket open");
__connError__ = false;
if (_timer_ != -1) {
clearTimeout(_timer_);
_timer_ = -1;
}
$connWaitBack.css("opacity", "1");
$connWaitBack.hide();
if (!_isReconnecting_) {
QUEUE.push({
callback: showWindow,
params: "choice_account"
});
send(requestToString(0, null));
} else {
send(requestToString(REQUESTS.C_RECONNECT, getGlobal("id")));
}
_isReconnecting_ = false;
}
//var _ping_pong_timeout = -1;
//var _ping_pong_time = 3000;
function gotMessage(data) {
//if (_ping_pong_timeout != -1) {
// clearTimeout(_ping_pong_timeout);
// _ping_pong_timeout = -1;
//}
//_ping_pong_timeout = setTimeout(function () {
// console.log("PING_PONG TIMEOUT");
// if (isDefined(getGlobal("round_remainingTime")) && getGlobal("round_remainingTime") > 0) {
// closeConnection();
// }
//}, _ping_pong_time);
console.log("Received: " + data);
var msg = data;
var code = msg.charCodeAt(0) - 'A'.charCodeAt(0);
msg = msg.substring(msg.indexOf("#") + 1);
var timestamp = parseInt(msg.substring(0, msg.indexOf("#")));
msg = msg.substring(msg.indexOf("#") + 1);
msg = JSON.parse(msg);
console.log("Received code: " + code + ". Handler: " + typeof(getRequestHandler(code)));
return {
callback: getRequestHandler(code),
paramsPair: [msg, timestamp]
};
}
var _reconnectTimeout_ = 10000;
var _isTimeout_ = false;
var _isReconnecting_ = false;
var _timer_ = -1;
function connectionClose(event) {
if (!__connError__) {
connCloseEvent();
}
}
function connectionError(event) {
__connError__ = true;
connCloseEvent();
}
function connCloseEvent() {
clearQueue();
console.log("CONN_CLOSE_EVENT: " + _isReconnecting_ + " :: " + _isTimeout_);
if (isDefined(getGlobal("game_players"))) {
if (!_isReconnecting_) {
var t = getGlobal("reconnect_timeout") || _reconnectTimeout_;
console.log("TIMER:: " + t);
_isTimeout_ = false;
setTimeout(function () {
_isTimeout_ = true;
}, t);
_isReconnecting_ = true;
$connWaitBack.show();
$connWaitBack.css("opacity", "0.3");
}
if (_isTimeout_) {
_isReconnecting_ = false;
if (_timer_ != -1) {
clearTimeout(_timer_);
}
_timer_ = -1;
$connWaitBack.css("opacity", "1");
connectionCloseError();
} else {
connection();
}
} else {
connectionCloseError();
}
}