Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add websocket ping interval option #519

Merged
merged 1 commit into from
Nov 7, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ static const struct lws_extension extensions[] = {
{NULL, NULL, NULL}};
#endif

#if LWS_LIBRARY_VERSION_NUMBER >= 4000000
static const uint32_t backoff_ms[] = { 1000, 2000, 3000, 4000, 5000 };
static lws_retry_bo_t retry = {
.retry_ms_table = backoff_ms,
.retry_ms_table_count = LWS_ARRAY_SIZE(backoff_ms),
.conceal_count = LWS_ARRAY_SIZE(backoff_ms),
.secs_since_valid_ping = 300,
.secs_since_valid_hangup = 300 + 7,
.jitter_percent = 0,
};
#endif

// command line options
static const struct option options[] = {
{"port", required_argument, NULL, 'p'},
Expand All @@ -52,6 +64,9 @@ static const struct option options[] = {
{"signal", required_argument, NULL, 's'},
{"index", required_argument, NULL, 'I'},
{"base-path", required_argument, NULL, 'b'},
#if LWS_LIBRARY_VERSION_NUMBER >= 4000000
{"ping-interval", required_argument, NULL, 'P'},
#endif
{"ipv6", no_argument, NULL, '6'},
{"ssl", no_argument, NULL, 'S'},
{"ssl-cert", required_argument, NULL, 'C'},
Expand All @@ -69,7 +84,13 @@ static const struct option options[] = {
{"version", no_argument, NULL, 'v'},
{"help", no_argument, NULL, 'h'},
{NULL, 0, 0, 0}};

#if LWS_LIBRARY_VERSION_NUMBER < 4000000
static const char *opt_string = "p:i:c:u:g:s:I:b:6aSC:K:A:Rt:T:Om:oBd:vh";
#endif
#if LWS_LIBRARY_VERSION_NUMBER >= 4000000
static const char *opt_string = "p:i:c:u:g:s:I:b:P:6aSC:K:A:Rt:T:Om:oBd:vh";
#endif

static void print_help() {
// clang-format off
Expand All @@ -95,6 +116,9 @@ static void print_help() {
" -B, --browser Open terminal with the default system browser\n"
" -I, --index Custom index.html path\n"
" -b, --base-path Expected base path for requests coming from a reverse proxy (eg: /mounted/here)\n"
#if LWS_LIBRARY_VERSION_NUMBER >= 4000000
" -P, --ping-interval Websocket ping interval(sec) (default: 300)\n"
#endif
#ifdef LWS_WITH_IPV6
" -6, --ipv6 Enable IPv6 support\n"
#endif
Expand Down Expand Up @@ -380,6 +404,17 @@ int main(int argc, char **argv) {
sc(ws) sc(index) sc(token) sc(parent)
#undef sc
} break;
#if LWS_LIBRARY_VERSION_NUMBER >= 4000000
case 'P':
if (atoi(optarg) <= 0) {
fprintf(stderr, "ttyd: invalid ping interval: %s\n", optarg);
return -1;
}
retry.secs_since_valid_ping = atoi(optarg);
retry.secs_since_valid_hangup = atoi(optarg) + 7;
info.retry_and_idle_policy = &retry;
break;
#endif
case '6':
info.options &= ~(LWS_SERVER_OPTION_DISABLE_IPV6);
break;
Expand Down