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

fix websocket connect error. retry 10 times, timeout 40s.

huqi 5 лет назад
Родитель
Сommit
06de369647
2 измененных файлов с 74 добавлено и 46 удалено
  1. 28 21
      o2web/gulpfile.js
  2. 46 25
      o2web/source/o2_core/o2/xDesktop/WebSocket.js

+ 28 - 21
o2web/gulpfile.js

@@ -79,8 +79,34 @@ function getAppTask(path, isMin, thisOptions) {
 
             gutil.log("Move-Uglify", ":", gutil.colors.green(gutil.colors.blue(path), gutil.colors.white('->'), dest));
 
-            return gulp.src(src_min)
-                .pipe(changed(dest))
+            var stream = gulp.src(src_min);
+            stream.on("end", function(){
+                let moveStream = gulp.src(src_move);
+                moveStream.on("end", function(){
+                    cb();
+                });
+
+                moveStream.pipe(changed(dest))
+                    .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
+                    .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
+                        host: option.host,
+                        user: option.user || 'anonymous',
+                        pass: option.pass || '@anonymous',
+                        port: option.port || 21,
+                        remotePath: (option.remotePath || '/') + path
+                    })))
+                    .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
+                        host: option.host,
+                        user: option.user || 'anonymous',
+                        pass: option.pass || null,
+                        port: option.port || 22,
+                        remotePath: (option.remotePath || '/') + path
+                    })))
+                    .pipe(gulp.dest(dest))
+                    .pipe(gutil.noop());
+
+            });
+            stream.pipe(changed(dest))
                 .pipe(uglify())
                 .pipe(rename({ extname: '.min.js' }))
                 .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
@@ -99,25 +125,6 @@ function getAppTask(path, isMin, thisOptions) {
                     remotePath: (option.remotePath || '/') + path
                 })))
                 .pipe(gulpif((option.ev == "dev" || option.ev == "pro") ,gulp.dest(dest)))
-
-                .pipe(gulp.src(src_move))
-                .pipe(changed(dest))
-                .pipe(gulpif((option.upload == 'local' && option.location != ''), gulp.dest(option.location + path + '/')))
-                .pipe(gulpif((option.upload == 'ftp' && option.host != ''), ftp({
-                    host: option.host,
-                    user: option.user || 'anonymous',
-                    pass: option.pass || '@anonymous',
-                    port: option.port || 21,
-                    remotePath: (option.remotePath || '/') + path
-                })))
-                .pipe(gulpif((option.upload == 'sftp' && option.host != ''), sftp({
-                    host: option.host,
-                    user: option.user || 'anonymous',
-                    pass: option.pass || null,
-                    port: option.port || 22,
-                    remotePath: (option.remotePath || '/') + path
-                })))
-                .pipe(gulp.dest(dest))
                 .pipe(gutil.noop());
 
 

+ 46 - 25
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,38 +38,43 @@ 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);
-                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){
+        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){
@@ -152,14 +160,25 @@ 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(){
+        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(){
         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;
@@ -168,14 +187,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);
@@ -186,15 +206,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();
         }