]> prime8.dev >> repos - ttyd.git/commitdiff
Do not reconnect if process exited, fixes #50
authorShuanglei Tao <tsl0922@gmail.com>
Sat, 20 May 2017 10:55:48 +0000 (18:55 +0800)
committerShuanglei Tao <tsl0922@gmail.com>
Sat, 20 May 2017 10:56:40 +0000 (18:56 +0800)
html/js/app.js
src/index.html
src/protocol.c

index f284d12139ad4ba537d0499765b197e5e1dd5c1d..2b5a0fbd6f23177cc1c1b9edf97825e6fe0a8b5a 100644 (file)
@@ -93,7 +93,8 @@
             }
             window.removeEventListener('beforeunload', unloadCallback);
             clearInterval(pingTimer);
-            if (autoReconnect > 0) {
+            // 1000: CLOSE_NORMAL
+            if (event.code !== 1000 && autoReconnect > 0) {
                 setTimeout(openWs, autoReconnect * 1000);
             }
         };
index e2ec8b24e317e59ae38ae8004e27ce3470614c26..26930a755eae4c562012d83a710c182c0a2baea1 100644 (file)
@@ -17,6 +17,6 @@ e||this.emit("scroll",this.ydisp),this.refresh(0,this.rows-1)},s.prototype.scrol
 <body>
        <div id="terminal-container"></div>
        <script src="auth_token.js"></script>
-       <script>!function(){var e,n,o,t=document.getElementById("terminal-container"),i="https:"==window.location.protocol,s=(i?"wss://":"ws://")+window.location.host+window.location.pathname+"ws",a="undefined"!=typeof tty_auth_token?tty_auth_token:null,c=["tty"],r=-1,l=function(){var i=new WebSocket(s,c),f=function(e){var n="Close terminal? this will also terminate the command.";return(e||window.event).returnValue=n,n};i.onopen=function(s){for(console.log("Websocket connection opened"),o=!1,i.send(JSON.stringify({AuthToken:a})),n=setInterval(d,3e4,i),"undefined"!=typeof e&&e.destroy(),e=new Terminal,e.on("resize",function(n){i.readyState===WebSocket.OPEN&&i.send("2"+JSON.stringify({columns:n.cols,rows:n.rows})),setTimeout(function(){e.showOverlay(n.cols+"x"+n.rows)},500)}),e.on("data",function(e){i.readyState===WebSocket.OPEN&&i.send("0"+e)}),e.on("open",function(){window.addEventListener("resize",function(n){e.fit()}),window.addEventListener("beforeunload",f),e.fit()});t.firstChild;)t.removeChild(t.firstChild);e.open(t,!0)},i.onmessage=function(n){var o=n.data.slice(1);switch(n.data[0]){case"0":e.writeUTF8(window.atob(o));break;case"1":break;case"2":document.title=o;break;case"3":var t=JSON.parse(o);Object.keys(t).forEach(function(n){console.log("Setting "+n+": "+t[n]),e.setOption(n,t[n])});break;case"4":r=JSON.parse(o),console.log("Enabling reconnect: "+r+" seconds")}},i.onclose=function(t){console.log("Websocket connection closed with code: "+t.code),e&&(e.off("data"),e.off("resize"),o||e.showOverlay("Connection Closed",null)),window.removeEventListener("beforeunload",f),clearInterval(n),r>0&&setTimeout(l,1e3*r)}},d=function(e){e.send("1")};l()}();</script>
+       <script>!function(){var e,n,o,t=document.getElementById("terminal-container"),i="https:"==window.location.protocol,s=(i?"wss://":"ws://")+window.location.host+window.location.pathname+"ws",a="undefined"!=typeof tty_auth_token?tty_auth_token:null,c=["tty"],r=-1,l=function(){var i=new WebSocket(s,c),f=function(e){var n="Close terminal? this will also terminate the command.";return(e||window.event).returnValue=n,n};i.onopen=function(s){for(console.log("Websocket connection opened"),o=!1,i.send(JSON.stringify({AuthToken:a})),n=setInterval(d,3e4,i),"undefined"!=typeof e&&e.destroy(),e=new Terminal,e.on("resize",function(n){i.readyState===WebSocket.OPEN&&i.send("2"+JSON.stringify({columns:n.cols,rows:n.rows})),setTimeout(function(){e.showOverlay(n.cols+"x"+n.rows)},500)}),e.on("data",function(e){i.readyState===WebSocket.OPEN&&i.send("0"+e)}),e.on("open",function(){window.addEventListener("resize",function(n){e.fit()}),window.addEventListener("beforeunload",f),e.fit()});t.firstChild;)t.removeChild(t.firstChild);e.open(t,!0)},i.onmessage=function(n){var o=n.data.slice(1);switch(n.data[0]){case"0":e.writeUTF8(window.atob(o));break;case"1":break;case"2":document.title=o;break;case"3":var t=JSON.parse(o);Object.keys(t).forEach(function(n){console.log("Setting "+n+": "+t[n]),e.setOption(n,t[n])});break;case"4":r=JSON.parse(o),console.log("Enabling reconnect: "+r+" seconds")}},i.onclose=function(t){console.log("Websocket connection closed with code: "+t.code),e&&(e.off("data"),e.off("resize"),o||e.showOverlay("Connection Closed",null)),window.removeEventListener("beforeunload",f),clearInterval(n),1e3!==t.code&&r>0&&setTimeout(l,1e3*r)}},d=function(e){e.send("1")};l()}();</script>
 </body>
 </html>
index 35128e7c857d844e694220ba941dc0601d4f7fdf..237d1d287b5d32f910d10eb0a8415a52f830ab6e 100644 (file)
@@ -227,6 +227,7 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
             if (!client->initialized) {
                 if (send_initial_message(wsi) < 0) {
                     tty_client_remove(client);
+                    lws_close_reason(wsi, LWS_CLOSE_STATUS_UNEXPECTED_CONDITION, NULL, 0);
                     return -1;
                 }
                 client->initialized = true;
@@ -241,6 +242,9 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
                     STAILQ_REMOVE_HEAD(&client->queue, list);
                     free(frame);
                     tty_client_remove(client);
+                    lws_close_reason(wsi,
+                                     frame->len == 0 ? LWS_CLOSE_STATUS_NORMAL : LWS_CLOSE_STATUS_UNEXPECTED_CONDITION,
+                                     NULL, 0);
                     return -1;
                 }
 
@@ -303,6 +307,7 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
                     if (write(client->pty, client->buffer + 1, client->len - 1) < client->len - 1) {
                         lwsl_err("write INPUT to pty\n");
                         tty_client_remove(client);
+                        lws_close_reason(wsi, LWS_CLOSE_STATUS_UNEXPECTED_CONDITION, NULL, 0);
                         return -1;
                     }
                     break;
@@ -312,6 +317,7 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
                         if (lws_write(wsi, &c, 1, LWS_WRITE_TEXT) != 1) {
                             lwsl_err("send PONG\n");
                             tty_client_remove(client);
+                            lws_close_reason(wsi, LWS_CLOSE_STATUS_UNEXPECTED_CONDITION, NULL, 0);
                             return -1;
                         }
                     }
@@ -338,6 +344,7 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
                         }
                         if (!client->authenticated) {
                             tty_client_remove(client);
+                            lws_close_reason(wsi, LWS_CLOSE_STATUS_POLICY_VIOLATION, NULL, 0);
                             return -1;
                         }
                     }
@@ -349,7 +356,8 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
                     break;
                 default:
                     lwsl_warn("unknown message type: %c\n", command);
-                    break;
+                    lws_close_reason(wsi, LWS_CLOSE_STATUS_INVALID_PAYLOAD, NULL, 0);
+                    return -1;
             }
 
             if (client->buffer != NULL) {