mirror of
https://github.com/tsl0922/ttyd.git
synced 2025-12-22 20:04:19 +01:00
server: use libwebsockets's built-in ping-pong
This commit is contained in:
@@ -147,7 +147,7 @@ var terminalContainer = document.getElementById('terminal-container'),
|
|||||||
textEncoder = new TextEncoder(),
|
textEncoder = new TextEncoder(),
|
||||||
authToken = (typeof tty_auth_token !== 'undefined') ? tty_auth_token : null,
|
authToken = (typeof tty_auth_token !== 'undefined') ? tty_auth_token : null,
|
||||||
autoReconnect = -1,
|
autoReconnect = -1,
|
||||||
term, pingTimer, title, wsError;
|
term, title, wsError;
|
||||||
|
|
||||||
var openWs = function() {
|
var openWs = function() {
|
||||||
var ws = new WebSocket(url, ['tty']);
|
var ws = new WebSocket(url, ['tty']);
|
||||||
@@ -198,9 +198,6 @@ var openWs = function() {
|
|||||||
console.log('Websocket connection opened');
|
console.log('Websocket connection opened');
|
||||||
wsError = false;
|
wsError = false;
|
||||||
sendMessage(JSON.stringify({AuthToken: authToken}));
|
sendMessage(JSON.stringify({AuthToken: authToken}));
|
||||||
pingTimer = setInterval(function() {
|
|
||||||
sendMessage('1');
|
|
||||||
}, 30 * 1000);
|
|
||||||
|
|
||||||
if (typeof term !== 'undefined') {
|
if (typeof term !== 'undefined') {
|
||||||
term.destroy();
|
term.destroy();
|
||||||
@@ -234,7 +231,7 @@ var openWs = function() {
|
|||||||
|
|
||||||
term.on('resize', function(size) {
|
term.on('resize', function(size) {
|
||||||
if (ws.readyState === WebSocket.OPEN) {
|
if (ws.readyState === WebSocket.OPEN) {
|
||||||
sendMessage('2' + JSON.stringify({columns: size.cols, rows: size.rows}));
|
sendMessage('1' + JSON.stringify({columns: size.cols, rows: size.rows}));
|
||||||
}
|
}
|
||||||
setTimeout(function() {
|
setTimeout(function() {
|
||||||
term.showOverlay(size.cols + 'x' + size.rows);
|
term.showOverlay(size.cols + 'x' + size.rows);
|
||||||
@@ -276,20 +273,18 @@ var openWs = function() {
|
|||||||
case '0':
|
case '0':
|
||||||
zsentry.consume(data);
|
zsentry.consume(data);
|
||||||
break;
|
break;
|
||||||
case '1': // pong
|
case '1':
|
||||||
break;
|
|
||||||
case '2':
|
|
||||||
title = textDecoder.decode(data);
|
title = textDecoder.decode(data);
|
||||||
document.title = title;
|
document.title = title;
|
||||||
break;
|
break;
|
||||||
case '3':
|
case '2':
|
||||||
var preferences = JSON.parse(textDecoder.decode(data));
|
var preferences = JSON.parse(textDecoder.decode(data));
|
||||||
Object.keys(preferences).forEach(function(key) {
|
Object.keys(preferences).forEach(function(key) {
|
||||||
console.log('Setting ' + key + ': ' + preferences[key]);
|
console.log('Setting ' + key + ': ' + preferences[key]);
|
||||||
term.setOption(key, preferences[key]);
|
term.setOption(key, preferences[key]);
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case '4':
|
case '3':
|
||||||
autoReconnect = JSON.parse(textDecoder.decode(data));
|
autoReconnect = JSON.parse(textDecoder.decode(data));
|
||||||
console.log('Enabling reconnect: ' + autoReconnect + ' seconds');
|
console.log('Enabling reconnect: ' + autoReconnect + ' seconds');
|
||||||
break;
|
break;
|
||||||
@@ -309,7 +304,6 @@ var openWs = function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
window.removeEventListener('beforeunload', unloadCallback);
|
window.removeEventListener('beforeunload', unloadCallback);
|
||||||
clearInterval(pingTimer);
|
|
||||||
// 1000: CLOSE_NORMAL
|
// 1000: CLOSE_NORMAL
|
||||||
if (event.code !== 1000 && autoReconnect > 0) {
|
if (event.code !== 1000 && autoReconnect > 0) {
|
||||||
setTimeout(openWs, autoReconnect * 1000);
|
setTimeout(openWs, autoReconnect * 1000);
|
||||||
|
|||||||
2
src/index.html
vendored
2
src/index.html
vendored
File diff suppressed because one or more lines are too long
@@ -273,15 +273,14 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
|
|||||||
|
|
||||||
case LWS_CALLBACK_RECEIVE:
|
case LWS_CALLBACK_RECEIVE:
|
||||||
if (client->buffer == NULL) {
|
if (client->buffer == NULL) {
|
||||||
client->buffer = xmalloc(len + 1);
|
client->buffer = xmalloc(len);
|
||||||
client->len = len;
|
client->len = len;
|
||||||
memcpy(client->buffer, in, len);
|
memcpy(client->buffer, in, len);
|
||||||
} else {
|
} else {
|
||||||
client->buffer = xrealloc(client->buffer, client->len + len + 1);
|
client->buffer = xrealloc(client->buffer, client->len + len);
|
||||||
memcpy(client->buffer + client->len, in, len);
|
memcpy(client->buffer + client->len, in, len);
|
||||||
client->len += len;
|
client->len += len;
|
||||||
}
|
}
|
||||||
client->buffer[client->len] = '\0';
|
|
||||||
|
|
||||||
const char command = client->buffer[0];
|
const char command = client->buffer[0];
|
||||||
|
|
||||||
@@ -302,24 +301,13 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason,
|
|||||||
break;
|
break;
|
||||||
if (server->readonly)
|
if (server->readonly)
|
||||||
return 0;
|
return 0;
|
||||||
if (write(client->pty, client->buffer + 1, client->len - 1) < client->len - 1) {
|
if (write(client->pty, client->buffer + 1, client->len - 1) == -1) {
|
||||||
lwsl_err("write INPUT to pty\n");
|
lwsl_err("write INPUT to pty: %d (%s)\n", errno, strerror(errno));
|
||||||
tty_client_remove(client);
|
tty_client_remove(client);
|
||||||
lws_close_reason(wsi, LWS_CLOSE_STATUS_UNEXPECTED_CONDITION, NULL, 0);
|
lws_close_reason(wsi, LWS_CLOSE_STATUS_UNEXPECTED_CONDITION, NULL, 0);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case PING:
|
|
||||||
{
|
|
||||||
unsigned char c = PONG;
|
|
||||||
if (lws_write(wsi, &c, 1, LWS_WRITE_BINARY) != 1) {
|
|
||||||
lwsl_err("send PONG\n");
|
|
||||||
tty_client_remove(client);
|
|
||||||
lws_close_reason(wsi, LWS_CLOSE_STATUS_UNEXPECTED_CONDITION, NULL, 0);
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case RESIZE_TERMINAL:
|
case RESIZE_TERMINAL:
|
||||||
if (parse_window_size(client->buffer + 1, &client->size) && client->pty > 0) {
|
if (parse_window_size(client->buffer + 1, &client->size) && client->pty > 0) {
|
||||||
if (ioctl(client->pty, TIOCSWINSZ, &client->size) == -1) {
|
if (ioctl(client->pty, TIOCSWINSZ, &client->size) == -1) {
|
||||||
|
|||||||
@@ -218,7 +218,6 @@ main(int argc, char **argv) {
|
|||||||
info.max_http_header_pool = 16;
|
info.max_http_header_pool = 16;
|
||||||
info.options = LWS_SERVER_OPTION_VALIDATE_UTF8;
|
info.options = LWS_SERVER_OPTION_VALIDATE_UTF8;
|
||||||
info.extensions = extensions;
|
info.extensions = extensions;
|
||||||
info.timeout_secs = 5;
|
|
||||||
|
|
||||||
int debug_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
|
int debug_level = LLL_ERR | LLL_WARN | LLL_NOTICE;
|
||||||
char iface[128] = "";
|
char iface[128] = "";
|
||||||
@@ -369,6 +368,9 @@ main(int argc, char **argv) {
|
|||||||
char server_hdr[128] = "";
|
char server_hdr[128] = "";
|
||||||
sprintf(server_hdr, "ttyd/%s (libwebsockets/%s)", TTYD_VERSION, LWS_LIBRARY_VERSION);
|
sprintf(server_hdr, "ttyd/%s (libwebsockets/%s)", TTYD_VERSION, LWS_LIBRARY_VERSION);
|
||||||
info.server_string = server_hdr;
|
info.server_string = server_hdr;
|
||||||
|
#if LWS_LIBRARY_VERSION_MINOR >= 1
|
||||||
|
info.ws_ping_pong_interval = 5;
|
||||||
|
#endif
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (strlen(iface) > 0) {
|
if (strlen(iface) > 0) {
|
||||||
|
|||||||
10
src/server.h
10
src/server.h
@@ -52,16 +52,14 @@
|
|||||||
|
|
||||||
// client message
|
// client message
|
||||||
#define INPUT '0'
|
#define INPUT '0'
|
||||||
#define PING '1'
|
#define RESIZE_TERMINAL '1'
|
||||||
#define RESIZE_TERMINAL '2'
|
|
||||||
#define JSON_DATA '{'
|
#define JSON_DATA '{'
|
||||||
|
|
||||||
// server message
|
// server message
|
||||||
#define OUTPUT '0'
|
#define OUTPUT '0'
|
||||||
#define PONG '1'
|
#define SET_WINDOW_TITLE '1'
|
||||||
#define SET_WINDOW_TITLE '2'
|
#define SET_PREFERENCES '2'
|
||||||
#define SET_PREFERENCES '3'
|
#define SET_RECONNECT '3'
|
||||||
#define SET_RECONNECT '4'
|
|
||||||
|
|
||||||
// websocket url path
|
// websocket url path
|
||||||
#define WS_PATH "/ws"
|
#define WS_PATH "/ws"
|
||||||
|
|||||||
Reference in New Issue
Block a user