Просмотр исходного кода

代码被覆盖回去了,重新修改websocket

huqi 5 лет назад
Родитель
Сommit
548da831f6
1 измененных файлов с 34 добавлено и 21 удалено
  1. 34 21
      o2web/source/o2_core/o2/xDesktop/WebSocket.js

+ 34 - 21
o2web/source/o2_core/o2/xDesktop/WebSocket.js

@@ -14,9 +14,12 @@ MWF.xDesktop.WebSocket = new Class({
 
         this.reConnect = true;
         this.checking = false;
-        this.heartTimeout = 30000;
-        this.checkingTimeout = 10000;
+        this.heartTimeout = 60000;
+        this.checkingTimeout = 4000;
         this.heartMsg = "heartbeat";
+        this.maxErrorCount = 10;
+        this.errorCount = 0;
+
 
         // var addressObj = layout.desktop.serviceAddressList["x_collaboration_assemble_websocket"];
         // this.ws = "ws://"+addressObj.host+(addressObj.port==80 ? "" : ":"+addressObj.port)+addressObj.context+"/ws/collaboration";
@@ -35,18 +38,21 @@ MWF.xDesktop.WebSocket = new Class({
         ///*暂时不启用WebSocket了------------
         //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){
-            this.ws = this.ws+"?x-token="+encodeURIComponent(Cookie.read("x-token"));
+            var ws = this.ws+"?x-token="+encodeURIComponent(Cookie.read("x-token"));
 
             try{
-                this.webSocket = new WebSocket(this.ws);
+                this.webSocket = new WebSocket(ws);
 
                 //this.webSocket = new WebSocket(this.ws);
                 this.webSocket.onopen = function (e){this.onOpen(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.onerror = function (e){this.onError(e);}.bind(this);
-                //---------------------------------*/\
+                //---------------------------------*/
             }catch(e){
                 //WebSocket.close();
                 //this.webSocket = new WebSocket(this.ws);
@@ -58,17 +64,18 @@ MWF.xDesktop.WebSocket = new Class({
                 //     //this.webSocket = new WebSocket(this.ws);
                 // }
             }
-            this.heartbeat();
         }
-
     },
     onOpen: function(e){
+        this.errorCount = 0;
         console.log("websocket is open, You can receive system messages");
+        this.heartbeat();
+
         //MWF.xDesktop.notice("success", {"x": "right", "y": "top"}, "websocket is open ...");
     },
     onClose: function(e){
         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 ...");
     },
     onMessage: function(e){
@@ -161,7 +168,10 @@ MWF.xDesktop.WebSocket = new Class({
         }
     },
     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 ...");
     },
     checkRetry: function(){
@@ -175,7 +185,8 @@ MWF.xDesktop.WebSocket = new Class({
         if (this.webSocket){
             this.close();
         }
-        this.initialize();
+        console.log("Retry connect to websocket server. ("+this.errorCount+"/"+this.maxErrorCount+")");
+        this.connect();
     },
     close: function(){
         this.reConnect = false;
@@ -184,14 +195,15 @@ MWF.xDesktop.WebSocket = new Class({
     },
     send: function(msg){
         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(){
         if (this.serverCheck) window.clearTimeout(this.serverCheck);
@@ -202,15 +214,16 @@ MWF.xDesktop.WebSocket = new Class({
     },
     sendHeartbeat: function(msg){
         if (!this.webSocket || this.webSocket.readyState != 1) {
+            if (this.serverCheck) window.clearTimeout(this.serverCheck);
             this.retry();
         }
         try{
             //console.log("send heartbeat ...");
             this.webSocket.send(msg);
-            this.serverCheck = window.setTimeout(function(){
-                this.retry();
-            }.bind(this), this.checkingTimeout);
+            this.checkRetry();
         }catch(e){
+            //console.log("send heartbeat error !!!");
+            if (this.serverCheck) window.clearTimeout(this.serverCheck);
             this.retry();
             //this.initialize();
         }
@@ -294,7 +307,7 @@ MWF.xDesktop.WebSocket = new Class({
     receiveReviewMessage: function(data){
         var content = MWF.LP.desktop.messsage.receiveReview+"《"+data.title+"》. ";
         content += "<br/><font style='color: #333; font-weight: bold'>"+MWF.LP.desktop.messsage.appliction+": </font><font style='color: #ea621f'>"+data.applicationName+"</font>;  "+
-        "<font style='color: #333; font-weight: bold'>"+MWF.LP.desktop.messsage.process+": </font><font style='color: #ea621f'>"+data.processName+"</font>";
+            "<font style='color: #333; font-weight: bold'>"+MWF.LP.desktop.messsage.process+": </font><font style='color: #ea621f'>"+data.processName+"</font>";
         var msg = {
             "subject": MWF.LP.desktop.messsage.reviewMessage,
             "content": content