From aa326f975dba9e1790e1dfab843db3f395fd3da4 Mon Sep 17 00:00:00 2001 From: "Sergio R. Caprile" Date: Fri, 15 Jul 2022 18:32:16 -0300 Subject: [PATCH] Added data push over WS example --- examples/webui-push-ws/Makefile | 20 ++++++++ examples/webui-push-ws/main.c | 54 ++++++++++++++++++++++ examples/webui-push-ws/web_root/index.html | 39 ++++++++++++++++ 3 files changed, 113 insertions(+) create mode 100644 examples/webui-push-ws/Makefile create mode 100644 examples/webui-push-ws/main.c create mode 100644 examples/webui-push-ws/web_root/index.html diff --git a/examples/webui-push-ws/Makefile b/examples/webui-push-ws/Makefile new file mode 100644 index 00000000..9addcb99 --- /dev/null +++ b/examples/webui-push-ws/Makefile @@ -0,0 +1,20 @@ +PROG ?= example +SSL = ? + +ifeq "$(SSL)" "MBEDTLS" +CFLAGS += -DMG_ENABLE_MBEDTLS=1 -lmbedtls -lmbedcrypto -lmbedx509 +endif + +ifeq "$(SSL)" "OPENSSL" +CFLAGS += -DMG_ENABLE_OPENSSL=1 -lssl -lcrypto +endif + +all: $(PROG) + $(DEBUGGER) ./$(PROG) $(ARGS) + + +$(PROG): main.c + $(CC) ../../mongoose.c -I../.. -W -Wall $(CFLAGS) $(EXTRA_CFLAGS) -o $(PROG) main.c + +clean: + rm -rf $(PROG) *.o *.dSYM *.gcov *.gcno *.gcda *.obj *.exe *.ilk *.pdb diff --git a/examples/webui-push-ws/main.c b/examples/webui-push-ws/main.c new file mode 100644 index 00000000..90fdf3a0 --- /dev/null +++ b/examples/webui-push-ws/main.c @@ -0,0 +1,54 @@ +// Copyright (c) 2020 Cesanta Software Limited +// All rights reserved +// +// Example Websocket server with timers. This is a simple Websocket echo +// server, which sends a message to all connected clients periodically, +// using timer API. + +#include "mongoose.h" + +static const char *s_listen_on = "http://localhost:8000"; +static const char *s_web_root = "web_root"; + +static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { + if (ev == MG_EV_HTTP_MSG) { + struct mg_http_message *hm = (struct mg_http_message *) ev_data; + if (mg_http_match_uri(hm, "/api/watch")) { + mg_ws_upgrade(c, hm, NULL); // Upgrade HTTP to Websocket + c->label[0] = 'W'; // Set some unique mark on the connection + } else { + struct mg_http_serve_opts opts = {.root_dir = s_web_root}; + mg_http_serve_dir(c, ev_data, &opts); + } + } + (void) fn_data; +} + +// Push to all watchers +static void push(struct mg_mgr *mgr, const char *name, const void *data) { + struct mg_connection *c; + for (c = mgr->conns; c != NULL; c = c->next) { + if (c->label[0] != 'W') continue; + mg_ws_printf(c, WEBSOCKET_OP_TEXT, "{%Q:%Q,%Q:%Q}", "name", name, "data", + data); + } +} + +static void timer_fn(void *arg) { + struct mg_mgr *mgr = (struct mg_mgr *) arg; + char buf[50]; + mg_snprintf(buf, sizeof(buf), "[%lu, %d]", (unsigned long) time(NULL), + 10 + (int) ((double) rand() * 10 / RAND_MAX)); + push(mgr, "metrics", buf); +} + +int main(void) { + struct mg_mgr mgr; // Event manager + mg_mgr_init(&mgr); // Initialise event manager + mg_log_set("2"); // Set debug log level + mg_timer_add(&mgr, 2000, MG_TIMER_REPEAT, timer_fn, &mgr); + mg_http_listen(&mgr, s_listen_on, fn, NULL); // Create HTTP listener + for (;;) mg_mgr_poll(&mgr, 500); // Infinite event loop + mg_mgr_free(&mgr); // Free manager resources + return 0; +} diff --git a/examples/webui-push-ws/web_root/index.html b/examples/webui-push-ws/web_root/index.html new file mode 100644 index 00000000..b8271e8e --- /dev/null +++ b/examples/webui-push-ws/web_root/index.html @@ -0,0 +1,39 @@ + + + +

Data push over WebSocket demo

+
Event log:
+
+ + +