Skip to content

Commit

Permalink
ping interval can be configured by the set_ping_interval function
Browse files Browse the repository at this point in the history
Default does not send Ping.

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
  • Loading branch information
Jianhui Zhao committed Feb 6, 2018
1 parent 024f7b5 commit 8ee65ca
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 12 additions & 2 deletions src/uwsc.c
Expand Up @@ -63,7 +63,7 @@ static void dispach_message(struct uwsc_client *cl)
break;
case WEBSOCKET_OP_PONG:
cl->wait_pingresp = false;
uloop_timeout_set(&cl->ping_timer, UWSC_PING_INTERVAL * 1000);
uloop_timeout_set(&cl->ping_timer, cl->ping_interval * 1000);
break;
case WEBSOCKET_OP_CLOSE:
uwsc_error(cl, 0);
Expand Down Expand Up @@ -251,7 +251,6 @@ static void __uwsc_notify_read(struct uwsc_client *cl, struct ustream *s)
if (cl->onopen)
cl->onopen(cl);

uloop_timeout_set(&cl->ping_timer, UWSC_PING_INTERVAL * 1000);
cl->state = CLIENT_STATE_MESSAGE;
} else if (cl->state == CLIENT_STATE_MESSAGE) {
if (!parse_frame(cl, (uint8_t *)data, len))
Expand Down Expand Up @@ -446,6 +445,16 @@ static void uwsc_ping_cb(struct uloop_timeout *timeout)
uloop_timeout_set(&cl->ping_timer, 1 * 1000);
}

static void uwsc_set_ping_interval(struct uwsc_client *cl, int interval)
{
cl->ping_interval = interval;

uloop_timeout_cancel(&cl->ping_timer);

if (interval > 0)
uloop_timeout_set(&cl->ping_timer, interval * 1000);
}

struct uwsc_client *uwsc_new_ssl(const char *url, const char *ca_crt_file, bool verify)
{
struct uwsc_client *cl = NULL;
Expand Down Expand Up @@ -475,6 +484,7 @@ struct uwsc_client *uwsc_new_ssl(const char *url, const char *ca_crt_file, bool
cl->free = uwsc_free;
cl->send = uwsc_send;
cl->ping = uwsc_ping;
cl->set_ping_interval = uwsc_set_ping_interval;
cl->ping_timer.cb = uwsc_ping_cb;
ustream_fd_init(&cl->sfd, sock);

Expand Down
4 changes: 2 additions & 2 deletions src/uwsc.h
Expand Up @@ -27,8 +27,6 @@
#include <libubox/ustream-ssl.h>
#endif

#define UWSC_PING_INTERVAL 30

enum uwsc_error_code {
UWSC_ERROR_WRITE,
UWSC_ERROR_INVALID_HEADER,
Expand Down Expand Up @@ -67,6 +65,7 @@ struct uwsc_client {
struct uwsc_frame frame;
struct uloop_timeout ping_timer;
bool wait_pingresp;
int ping_interval;
enum uwsc_error_code error;

#if (UWSC_SSL_SUPPORT)
Expand All @@ -77,6 +76,7 @@ struct uwsc_client {
#endif

void (*onopen)(struct uwsc_client *cl);
void (*set_ping_interval)(struct uwsc_client *cl, int interval);
void (*onmessage)(struct uwsc_client *cl, void *data, uint64_t len, enum websocket_op op);
void (*onerror)(struct uwsc_client *cl);
void (*onclose)(struct uwsc_client *cl);
Expand Down

0 comments on commit 8ee65ca

Please sign in to comment.