From 26f8b17891b6f0d0dc5503d2760894e3ab1dc28c Mon Sep 17 00:00:00 2001 From: Shuanglei Tao Date: Thu, 11 Jan 2018 22:15:49 +0800 Subject: [PATCH] server: use libwebsockets's built-in ping-pong --- html/js/app.js | 16 +++++----------- src/index.html | 2 +- src/protocol.c | 20 ++++---------------- src/server.c | 4 +++- src/server.h | 10 ++++------ 5 files changed, 17 insertions(+), 35 deletions(-) diff --git a/html/js/app.js b/html/js/app.js index 8c3bb7a..a53277b 100644 --- a/html/js/app.js +++ b/html/js/app.js @@ -147,7 +147,7 @@ var terminalContainer = document.getElementById('terminal-container'), textEncoder = new TextEncoder(), authToken = (typeof tty_auth_token !== 'undefined') ? tty_auth_token : null, autoReconnect = -1, - term, pingTimer, title, wsError; + term, title, wsError; var openWs = function() { var ws = new WebSocket(url, ['tty']); @@ -198,9 +198,6 @@ var openWs = function() { console.log('Websocket connection opened'); wsError = false; sendMessage(JSON.stringify({AuthToken: authToken})); - pingTimer = setInterval(function() { - sendMessage('1'); - }, 30 * 1000); if (typeof term !== 'undefined') { term.destroy(); @@ -234,7 +231,7 @@ var openWs = function() { term.on('resize', function(size) { 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() { term.showOverlay(size.cols + 'x' + size.rows); @@ -276,20 +273,18 @@ var openWs = function() { case '0': zsentry.consume(data); break; - case '1': // pong - break; - case '2': + case '1': title = textDecoder.decode(data); document.title = title; break; - case '3': + case '2': var preferences = JSON.parse(textDecoder.decode(data)); Object.keys(preferences).forEach(function(key) { console.log('Setting ' + key + ': ' + preferences[key]); term.setOption(key, preferences[key]); }); break; - case '4': + case '3': autoReconnect = JSON.parse(textDecoder.decode(data)); console.log('Enabling reconnect: ' + autoReconnect + ' seconds'); break; @@ -309,7 +304,6 @@ var openWs = function() { } } window.removeEventListener('beforeunload', unloadCallback); - clearInterval(pingTimer); // 1000: CLOSE_NORMAL if (event.code !== 1000 && autoReconnect > 0) { setTimeout(openWs, autoReconnect * 1000); diff --git a/src/index.html b/src/index.html index 1bdfcca..8e7e2a2 100644 --- a/src/index.html +++ b/src/index.html @@ -51,6 +51,6 @@ blockquote,body,dd,dl,dt,fieldset,figure,h1,h2,h3,h4,h5,h6,hr,html,iframe,legend - + diff --git a/src/protocol.c b/src/protocol.c index 845718c..65764ab 100644 --- a/src/protocol.c +++ b/src/protocol.c @@ -273,15 +273,14 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason, case LWS_CALLBACK_RECEIVE: if (client->buffer == NULL) { - client->buffer = xmalloc(len + 1); + client->buffer = xmalloc(len); client->len = len; memcpy(client->buffer, in, len); } 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); client->len += len; } - client->buffer[client->len] = '\0'; const char command = client->buffer[0]; @@ -302,24 +301,13 @@ callback_tty(struct lws *wsi, enum lws_callback_reasons reason, break; if (server->readonly) return 0; - if (write(client->pty, client->buffer + 1, client->len - 1) < client->len - 1) { - lwsl_err("write INPUT to pty\n"); + if (write(client->pty, client->buffer + 1, client->len - 1) == -1) { + lwsl_err("write INPUT to pty: %d (%s)\n", errno, strerror(errno)); tty_client_remove(client); lws_close_reason(wsi, LWS_CLOSE_STATUS_UNEXPECTED_CONDITION, NULL, 0); return -1; } 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: if (parse_window_size(client->buffer + 1, &client->size) && client->pty > 0) { if (ioctl(client->pty, TIOCSWINSZ, &client->size) == -1) { diff --git a/src/server.c b/src/server.c index c1a17a5..a16bab0 100644 --- a/src/server.c +++ b/src/server.c @@ -218,7 +218,6 @@ main(int argc, char **argv) { info.max_http_header_pool = 16; info.options = LWS_SERVER_OPTION_VALIDATE_UTF8; info.extensions = extensions; - info.timeout_secs = 5; int debug_level = LLL_ERR | LLL_WARN | LLL_NOTICE; char iface[128] = ""; @@ -369,6 +368,9 @@ main(int argc, char **argv) { char server_hdr[128] = ""; sprintf(server_hdr, "ttyd/%s (libwebsockets/%s)", TTYD_VERSION, LWS_LIBRARY_VERSION); info.server_string = server_hdr; +#if LWS_LIBRARY_VERSION_MINOR >= 1 + info.ws_ping_pong_interval = 5; +#endif #endif if (strlen(iface) > 0) { diff --git a/src/server.h b/src/server.h index c357315..881b4e8 100644 --- a/src/server.h +++ b/src/server.h @@ -52,16 +52,14 @@ // client message #define INPUT '0' -#define PING '1' -#define RESIZE_TERMINAL '2' +#define RESIZE_TERMINAL '1' #define JSON_DATA '{' // server message #define OUTPUT '0' -#define PONG '1' -#define SET_WINDOW_TITLE '2' -#define SET_PREFERENCES '3' -#define SET_RECONNECT '4' +#define SET_WINDOW_TITLE '1' +#define SET_PREFERENCES '2' +#define SET_RECONNECT '3' // websocket url path #define WS_PATH "/ws" -- 2.43.4