mirror of
https://github.com/tsl0922/ttyd.git
synced 2025-12-22 11:54:19 +01:00
Do not reconnect if process exited, fixes #50
This commit is contained in:
@@ -93,7 +93,8 @@
|
|||||||
}
|
}
|
||||||
window.removeEventListener('beforeunload', unloadCallback);
|
window.removeEventListener('beforeunload', unloadCallback);
|
||||||
clearInterval(pingTimer);
|
clearInterval(pingTimer);
|
||||||
if (autoReconnect > 0) {
|
// 1000: CLOSE_NORMAL
|
||||||
|
if (event.code !== 1000 && autoReconnect > 0) {
|
||||||
setTimeout(openWs, autoReconnect * 1000);
|
setTimeout(openWs, autoReconnect * 1000);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
2
src/index.html
vendored
2
src/index.html
vendored
@@ -17,6 +17,6 @@ e||this.emit("scroll",this.ydisp),this.refresh(0,this.rows-1)},s.prototype.scrol
|
|||||||
<body>
|
<body>
|
||||||
<div id="terminal-container"></div>
|
<div id="terminal-container"></div>
|
||||||
<script src="auth_token.js"></script>
|
<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>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
@@ -227,6 +227,7 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
|
|||||||
if (!client->initialized) {
|
if (!client->initialized) {
|
||||||
if (send_initial_message(wsi) < 0) {
|
if (send_initial_message(wsi) < 0) {
|
||||||
tty_client_remove(client);
|
tty_client_remove(client);
|
||||||
|
lws_close_reason(wsi, LWS_CLOSE_STATUS_UNEXPECTED_CONDITION, NULL, 0);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
client->initialized = true;
|
client->initialized = true;
|
||||||
@@ -241,6 +242,9 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
|
|||||||
STAILQ_REMOVE_HEAD(&client->queue, list);
|
STAILQ_REMOVE_HEAD(&client->queue, list);
|
||||||
free(frame);
|
free(frame);
|
||||||
tty_client_remove(client);
|
tty_client_remove(client);
|
||||||
|
lws_close_reason(wsi,
|
||||||
|
frame->len == 0 ? LWS_CLOSE_STATUS_NORMAL : LWS_CLOSE_STATUS_UNEXPECTED_CONDITION,
|
||||||
|
NULL, 0);
|
||||||
return -1;
|
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) {
|
if (write(client->pty, client->buffer + 1, client->len - 1) < client->len - 1) {
|
||||||
lwsl_err("write INPUT to pty\n");
|
lwsl_err("write INPUT to pty\n");
|
||||||
tty_client_remove(client);
|
tty_client_remove(client);
|
||||||
|
lws_close_reason(wsi, LWS_CLOSE_STATUS_UNEXPECTED_CONDITION, NULL, 0);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
break;
|
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) {
|
if (lws_write(wsi, &c, 1, LWS_WRITE_TEXT) != 1) {
|
||||||
lwsl_err("send PONG\n");
|
lwsl_err("send PONG\n");
|
||||||
tty_client_remove(client);
|
tty_client_remove(client);
|
||||||
|
lws_close_reason(wsi, LWS_CLOSE_STATUS_UNEXPECTED_CONDITION, NULL, 0);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -338,6 +344,7 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
|
|||||||
}
|
}
|
||||||
if (!client->authenticated) {
|
if (!client->authenticated) {
|
||||||
tty_client_remove(client);
|
tty_client_remove(client);
|
||||||
|
lws_close_reason(wsi, LWS_CLOSE_STATUS_POLICY_VIOLATION, NULL, 0);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -349,7 +356,8 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
|
|||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
lwsl_warn("unknown message type: %c\n", command);
|
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) {
|
if (client->buffer != NULL) {
|
||||||
|
|||||||
Reference in New Issue
Block a user