diff --git a/example/example.c b/example/example.c index ceb3baf..b378809 100644 --- a/example/example.c +++ b/example/example.c @@ -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); } } diff --git a/src/uwsc.c b/src/uwsc.c index bac6321..c5f8134 100644 --- a/src/uwsc.c +++ b/src/uwsc.c @@ -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); } @@ -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; diff --git a/src/uwsc.h b/src/uwsc.h index f031338..37b9648 100644 --- a/src/uwsc.h +++ b/src/uwsc.h @@ -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); };