From 09d28f55707750338d31f8f7856c5018f3fb535a Mon Sep 17 00:00:00 2001 From: Sergey Lyubka Date: Tue, 2 Nov 2021 16:53:57 +0000 Subject: [PATCH] Print a clarifying message at start --- examples/websocket-server/main.c | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/examples/websocket-server/main.c b/examples/websocket-server/main.c index 392f4a51..f61dee2c 100644 --- a/examples/websocket-server/main.c +++ b/examples/websocket-server/main.c @@ -8,15 +8,17 @@ #include "mongoose.h" -static const char *s_listen_on = "http://localhost:8000"; -static const char *s_web_directory = "."; +static const char *s_listen_on = "ws://localhost:8000"; +static const char *s_web_root = "."; // This RESTful server implements the following endpoints: // /websocket - upgrade to Websocket, and implement websocket echo server // /api/rest - respond with JSON string {"result": 123} -// any other URI serves static files from s_web_directory +// any other URI serves static files from s_web_root static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { - if (ev == MG_EV_HTTP_MSG) { + if (ev == MG_EV_OPEN) { + // c->is_hexdumping = 1; + } else if (ev == MG_EV_HTTP_MSG) { struct mg_http_message *hm = (struct mg_http_message *) ev_data; if (mg_http_match_uri(hm, "/websocket")) { // Upgrade to websocket. From now on, a connection is a full-duplex @@ -27,7 +29,7 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123); } else { // Serve static files - struct mg_http_serve_opts opts = {.root_dir = s_web_directory}; + struct mg_http_serve_opts opts = {.root_dir = s_web_root}; mg_http_serve_dir(c, ev_data, &opts); } } else if (ev == MG_EV_WS_MSG) { @@ -40,8 +42,9 @@ static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data) { } int main(void) { - struct mg_mgr mgr; // Event manager - mg_mgr_init(&mgr); // Initialise event manager + struct mg_mgr mgr; // Event manager + mg_mgr_init(&mgr); // Initialise event manager + printf("Starting WS listener on %s/websocket\n", s_listen_on); mg_http_listen(&mgr, s_listen_on, fn, NULL); // Create HTTP listener for (;;) mg_mgr_poll(&mgr, 1000); // Infinite event loop mg_mgr_free(&mgr);