|
@@ -14,9 +14,12 @@ MWF.xDesktop.WebSocket = new Class({
|
|
|
|
|
|
|
|
this.reConnect = true;
|
|
this.reConnect = true;
|
|
|
this.checking = false;
|
|
this.checking = false;
|
|
|
- this.heartTimeout = 30000;
|
|
|
|
|
- this.checkingTimeout = 10000;
|
|
|
|
|
|
|
+ this.heartTimeout = 60000;
|
|
|
|
|
+ this.checkingTimeout = 4000;
|
|
|
this.heartMsg = "heartbeat";
|
|
this.heartMsg = "heartbeat";
|
|
|
|
|
+ this.maxErrorCount = 10;
|
|
|
|
|
+ this.errorCount = 0;
|
|
|
|
|
+
|
|
|
|
|
|
|
|
// var addressObj = layout.desktop.serviceAddressList["x_collaboration_assemble_websocket"];
|
|
// var addressObj = layout.desktop.serviceAddressList["x_collaboration_assemble_websocket"];
|
|
|
// this.ws = "ws://"+addressObj.host+(addressObj.port==80 ? "" : ":"+addressObj.port)+addressObj.context+"/ws/collaboration";
|
|
// this.ws = "ws://"+addressObj.host+(addressObj.port==80 ? "" : ":"+addressObj.port)+addressObj.context+"/ws/collaboration";
|
|
@@ -35,38 +38,43 @@ MWF.xDesktop.WebSocket = new Class({
|
|
|
///*暂时不启用WebSocket了------------
|
|
///*暂时不启用WebSocket了------------
|
|
|
//this.ws = this.ws+"?x-token="+encodeURIComponent(Cookie.read("x-token"))+"&authorization="+encodeURIComponent(Cookie.read("x-token"));
|
|
//this.ws = this.ws+"?x-token="+encodeURIComponent(Cookie.read("x-token"))+"&authorization="+encodeURIComponent(Cookie.read("x-token"));
|
|
|
|
|
|
|
|
|
|
+ this.connect();
|
|
|
|
|
+ },
|
|
|
|
|
+ connect: function(){
|
|
|
if (layout.config.webSocketEnable){
|
|
if (layout.config.webSocketEnable){
|
|
|
- this.ws = this.ws+"?x-token="+encodeURIComponent(Cookie.read("x-token"));
|
|
|
|
|
|
|
+ var ws = this.ws+"?x-token="+encodeURIComponent(Cookie.read("x-token"));
|
|
|
|
|
|
|
|
try{
|
|
try{
|
|
|
- this.webSocket = new WebSocket(this.ws);
|
|
|
|
|
|
|
+ this.webSocket = new WebSocket(ws);
|
|
|
|
|
|
|
|
//this.webSocket = new WebSocket(this.ws);
|
|
//this.webSocket = new WebSocket(this.ws);
|
|
|
this.webSocket.onopen = function (e){this.onOpen(e);}.bind(this);
|
|
this.webSocket.onopen = function (e){this.onOpen(e);}.bind(this);
|
|
|
this.webSocket.onclose = function (e){this.onClose(e);}.bind(this);
|
|
this.webSocket.onclose = function (e){this.onClose(e);}.bind(this);
|
|
|
this.webSocket.onmessage = function (e){this.onMessage(e);}.bind(this);
|
|
this.webSocket.onmessage = function (e){this.onMessage(e);}.bind(this);
|
|
|
this.webSocket.onerror = function (e){this.onError(e);}.bind(this);
|
|
this.webSocket.onerror = function (e){this.onError(e);}.bind(this);
|
|
|
- //---------------------------------*/\
|
|
|
|
|
|
|
+ //---------------------------------*/
|
|
|
}catch(e){
|
|
}catch(e){
|
|
|
//WebSocket.close();
|
|
//WebSocket.close();
|
|
|
//this.webSocket = new WebSocket(this.ws);
|
|
//this.webSocket = new WebSocket(this.ws);
|
|
|
- console.log("Unable to connect to the websocket server, will retry in "+(this.heartTimeout/1000)+" seconds");
|
|
|
|
|
- if (this.webSocket){
|
|
|
|
|
- this.close();
|
|
|
|
|
- //this.webSocket = new WebSocket(this.ws);
|
|
|
|
|
- }
|
|
|
|
|
|
|
+ console.log("Unable to connect to the websocket server, will retry in "+(this.checkingTimeout/1000)+" seconds");
|
|
|
|
|
+ this.checkRetry();
|
|
|
|
|
+ // if (this.webSocket){
|
|
|
|
|
+ // this.close();
|
|
|
|
|
+ // //this.webSocket = new WebSocket(this.ws);
|
|
|
|
|
+ // }
|
|
|
}
|
|
}
|
|
|
- this.heartbeat();
|
|
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
},
|
|
},
|
|
|
onOpen: function(e){
|
|
onOpen: function(e){
|
|
|
|
|
+ this.errorCount = 0;
|
|
|
console.log("websocket is open, You can receive system messages");
|
|
console.log("websocket is open, You can receive system messages");
|
|
|
|
|
+ this.heartbeat();
|
|
|
|
|
+
|
|
|
//MWF.xDesktop.notice("success", {"x": "right", "y": "top"}, "websocket is open ...");
|
|
//MWF.xDesktop.notice("success", {"x": "right", "y": "top"}, "websocket is open ...");
|
|
|
},
|
|
},
|
|
|
onClose: function(e){
|
|
onClose: function(e){
|
|
|
console.log("websocket is closed. ");
|
|
console.log("websocket is closed. ");
|
|
|
- if (this.reConnect) this.initialize();
|
|
|
|
|
|
|
+ //if (this.reConnect) this.checkRetry();
|
|
|
//MWF.xDesktop.notice("success", {"x": "right", "y": "top"}, "websocket is closed ...");
|
|
//MWF.xDesktop.notice("success", {"x": "right", "y": "top"}, "websocket is closed ...");
|
|
|
},
|
|
},
|
|
|
onMessage: function(e){
|
|
onMessage: function(e){
|
|
@@ -152,14 +160,25 @@ MWF.xDesktop.WebSocket = new Class({
|
|
|
}
|
|
}
|
|
|
},
|
|
},
|
|
|
onError: function(e){
|
|
onError: function(e){
|
|
|
- console.log("websocket is error ...");
|
|
|
|
|
|
|
+ this.errorCount++;
|
|
|
|
|
+ //console.log(e);
|
|
|
|
|
+ console.log("Unable to connect to the websocket server, will retry in "+(this.checkingTimeout/1000)+" seconds.");
|
|
|
|
|
+ this.checkRetry();
|
|
|
//MWF.xDesktop.notice("success", {"x": "right", "y": "top"}, "websocket is error ...");
|
|
//MWF.xDesktop.notice("success", {"x": "right", "y": "top"}, "websocket is error ...");
|
|
|
},
|
|
},
|
|
|
|
|
+ checkRetry: function(){
|
|
|
|
|
+ if (this.serverCheck) window.clearTimeout(this.serverCheck);
|
|
|
|
|
+ if (this.heartbeatCheck) window.clearTimeout(this.heartbeatCheck);
|
|
|
|
|
+ if (this.errorCount < this.maxErrorCount) this.serverCheck = window.setTimeout(function(){
|
|
|
|
|
+ this.retry();
|
|
|
|
|
+ }.bind(this), this.checkingTimeout);
|
|
|
|
|
+ },
|
|
|
retry: function(){
|
|
retry: function(){
|
|
|
if (this.webSocket){
|
|
if (this.webSocket){
|
|
|
this.close();
|
|
this.close();
|
|
|
}
|
|
}
|
|
|
- this.initialize();
|
|
|
|
|
|
|
+ console.log("Retry connect to websocket server. ("+this.errorCount+"/"+this.maxErrorCount+")");
|
|
|
|
|
+ this.connect();
|
|
|
},
|
|
},
|
|
|
close: function(){
|
|
close: function(){
|
|
|
this.reConnect = false;
|
|
this.reConnect = false;
|
|
@@ -168,14 +187,15 @@ MWF.xDesktop.WebSocket = new Class({
|
|
|
},
|
|
},
|
|
|
send: function(msg){
|
|
send: function(msg){
|
|
|
if (!this.webSocket || this.webSocket.readyState != 1) {
|
|
if (!this.webSocket || this.webSocket.readyState != 1) {
|
|
|
- this.initialize();
|
|
|
|
|
- }
|
|
|
|
|
- try{
|
|
|
|
|
- this.webSocket.send(JSON.encode(msg));
|
|
|
|
|
- }catch(e){
|
|
|
|
|
- this.initialize();
|
|
|
|
|
- this.webSocket.send(JSON.encode(msg));
|
|
|
|
|
|
|
+ if (this.serverCheck) window.clearTimeout(this.serverCheck);
|
|
|
|
|
+ this.retry();
|
|
|
}
|
|
}
|
|
|
|
|
+ // try{
|
|
|
|
|
+ this.webSocket.send(JSON.encode(msg));
|
|
|
|
|
+ // }catch(e){
|
|
|
|
|
+ // this.retry();
|
|
|
|
|
+ // this.webSocket.send(JSON.encode(msg));
|
|
|
|
|
+ // }
|
|
|
},
|
|
},
|
|
|
heartbeat: function(){
|
|
heartbeat: function(){
|
|
|
if (this.serverCheck) window.clearTimeout(this.serverCheck);
|
|
if (this.serverCheck) window.clearTimeout(this.serverCheck);
|
|
@@ -186,15 +206,16 @@ MWF.xDesktop.WebSocket = new Class({
|
|
|
},
|
|
},
|
|
|
sendHeartbeat: function(msg){
|
|
sendHeartbeat: function(msg){
|
|
|
if (!this.webSocket || this.webSocket.readyState != 1) {
|
|
if (!this.webSocket || this.webSocket.readyState != 1) {
|
|
|
|
|
+ if (this.serverCheck) window.clearTimeout(this.serverCheck);
|
|
|
this.retry();
|
|
this.retry();
|
|
|
}
|
|
}
|
|
|
try{
|
|
try{
|
|
|
//console.log("send heartbeat ...");
|
|
//console.log("send heartbeat ...");
|
|
|
this.webSocket.send(msg);
|
|
this.webSocket.send(msg);
|
|
|
- this.serverCheck = window.setTimeout(function(){
|
|
|
|
|
- this.retry();
|
|
|
|
|
- }.bind(this), this.checkingTimeout);
|
|
|
|
|
|
|
+ this.checkRetry();
|
|
|
}catch(e){
|
|
}catch(e){
|
|
|
|
|
+ //console.log("send heartbeat error !!!");
|
|
|
|
|
+ if (this.serverCheck) window.clearTimeout(this.serverCheck);
|
|
|
this.retry();
|
|
this.retry();
|
|
|
//this.initialize();
|
|
//this.initialize();
|
|
|
}
|
|
}
|