diff --git a/mongoose.c b/mongoose.c index cd60e9af..f39efa7d 100644 --- a/mongoose.c +++ b/mongoose.c @@ -5702,6 +5702,24 @@ struct ws_msg { size_t data_len; }; +size_t mg_ws_vprintf(struct mg_connection *c, int op, const char *fmt, + va_list ap) { + char mem[256], *buf = mem; + size_t len = mg_vasprintf(&buf, sizeof(mem), fmt, ap); + len = mg_ws_send(c, buf, len, op); + if (buf != mem) free(buf); + return len; +} + +size_t mg_ws_printf(struct mg_connection *c, int op, const char *fmt, ...) { + size_t len = 0; + va_list ap; + va_start(ap, fmt); + len = mg_ws_vprintf(c, op, fmt, ap); + va_end(ap); + return len; +} + static void ws_handshake(struct mg_connection *c, const struct mg_str *wskey, const struct mg_str *wsproto, const char *fmt, va_list ap) { diff --git a/mongoose.h b/mongoose.h index 02910e45..fbe26f23 100644 --- a/mongoose.h +++ b/mongoose.h @@ -1178,6 +1178,8 @@ void mg_ws_upgrade(struct mg_connection *, struct mg_http_message *, const char *fmt, ...); size_t mg_ws_send(struct mg_connection *, const char *buf, size_t len, int op); size_t mg_ws_wrap(struct mg_connection *, size_t len, int op); +size_t mg_ws_printf(struct mg_connection *c, int op, const char *fmt, ...); +size_t mg_ws_vprintf(struct mg_connection *c, int op, const char *fmt, va_list); diff --git a/src/ws.c b/src/ws.c index fed44aa0..243652b5 100644 --- a/src/ws.c +++ b/src/ws.c @@ -13,6 +13,24 @@ struct ws_msg { size_t data_len; }; +size_t mg_ws_vprintf(struct mg_connection *c, int op, const char *fmt, + va_list ap) { + char mem[256], *buf = mem; + size_t len = mg_vasprintf(&buf, sizeof(mem), fmt, ap); + len = mg_ws_send(c, buf, len, op); + if (buf != mem) free(buf); + return len; +} + +size_t mg_ws_printf(struct mg_connection *c, int op, const char *fmt, ...) { + size_t len = 0; + va_list ap; + va_start(ap, fmt); + len = mg_ws_vprintf(c, op, fmt, ap); + va_end(ap); + return len; +} + static void ws_handshake(struct mg_connection *c, const struct mg_str *wskey, const struct mg_str *wsproto, const char *fmt, va_list ap) { diff --git a/src/ws.h b/src/ws.h index a2ae4157..628723e5 100644 --- a/src/ws.h +++ b/src/ws.h @@ -21,3 +21,5 @@ void mg_ws_upgrade(struct mg_connection *, struct mg_http_message *, const char *fmt, ...); size_t mg_ws_send(struct mg_connection *, const char *buf, size_t len, int op); size_t mg_ws_wrap(struct mg_connection *, size_t len, int op); +size_t mg_ws_printf(struct mg_connection *c, int op, const char *fmt, ...); +size_t mg_ws_vprintf(struct mg_connection *c, int op, const char *fmt, va_list);