1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
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();
}
}