Skip to content

Commit

Permalink
Provide function to send closed message
Browse files Browse the repository at this point in the history
cl->send_close(cl, UWSC_CLOSE_STATUS_NORMAL, "ByeBye");

Signed-off-by: Jianhui Zhao <jianhuizhao329@gmail.com>
  • Loading branch information
Jianhui Zhao committed Jan 12, 2019
1 parent eb6dfd4 commit eaacc64
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
5 changes: 4 additions & 1 deletion example/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ static void stdin_read_cb(struct ev_loop *loop, struct ev_io *w, int revents)
if (n > 1) {
buf[n - 1] = 0;

cl->send(cl, buf, strlen(buf) + 1, UWSC_OP_TEXT);
if (buf[0] == 'q')
cl->send_close(cl, UWSC_CLOSE_STATUS_NORMAL, "ByeBye");
else
cl->send(cl, buf, strlen(buf) + 1, UWSC_OP_TEXT);
}
}

Expand Down
5 changes: 4 additions & 1 deletion src/uwsc.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@ static int uwsc_send_close(struct uwsc_client *cl, int code, const char *reason)

code = htobe16(code & 0xFFFF);
memcpy(buf, &code, 2);
strncpy(&buf[2], reason, sizeof(buf) - 3);

if (reason)
strncpy(&buf[2], reason, sizeof(buf) - 3);

return cl->send(cl, buf, strlen(buf + 2) + 2, UWSC_OP_CLOSE);
}
Expand Down Expand Up @@ -594,6 +596,7 @@ struct uwsc_client *uwsc_new(struct ev_loop *loop, const char *url, int ping_int
cl->sock = sock;
cl->send = uwsc_send;
cl->send_ex = uwsc_send_ex;
cl->send_close = uwsc_send_close;
cl->ping = uwsc_ping;
cl->start_time = ev_now(loop);
cl->ping_interval = ping_interval;
Expand Down
1 change: 1 addition & 0 deletions src/uwsc.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ struct uwsc_client {

int (*send)(struct uwsc_client *cl, const void *data, size_t len, int op);
int (*send_ex)(struct uwsc_client *cl, int op, int num, ...);
int (*send_close)(struct uwsc_client *cl, int code, const char *reason);
void (*ping)(struct uwsc_client *cl);
};

Expand Down

0 comments on commit eaacc64

Please sign in to comment.