Merge pull request #1572 from cesanta/timers

Modify times example
This commit is contained in:
Sergey Lyubka 2022-06-01 17:22:24 +01:00 committed by GitHub
commit d7b7a0a577
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 3 deletions

View File

@ -19,7 +19,7 @@ mingw:
gcc ../../mongoose.c main.c -I../.. -W -Wall -DMG_ENABLE_IPV6=1 -DMG_ENABLE_LINES=1 -D_POSIX_C_SOURCE=200000L -lws2_32 -o mongoose.exe
linux: example
linux: CFLAGS += -O2
#linux: CFLAGS += -O2 -fsanitize=address,undefined,shift,null,return,bounds,alignment,object-size,bool,enum -static-libasan
linux: CC = $(LIN) cc
clean:

View File

@ -13,13 +13,20 @@
#include "mongoose.h"
static const char *s_listen_on = "http://localhost:8000";
static const char *s_web_root = "web_root";
// This RESTful server implements the following endpoints:
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;
mg_ws_upgrade(c, hm, NULL);
c->label[0] = 'W'; // Set some unique mark on a connection
if (mg_http_match_uri(hm, "/websocket")) {
mg_ws_upgrade(c, hm, NULL);
c->label[0] = 'W'; // Set some unique mark on a connection
} else {
// Serve static files
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) {
// Got websocket frame. Received data is wm->data. Echo it back!
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;

View File

@ -0,0 +1,34 @@
<!DOCTYPE html>
<html lang="en">
<body>
<h1>Websocket test client</h1>
<input id="url" type="text" placeholder="Type URL" value="ws://localhost:8000/websocket" style="width:20em;" />
<button id="connect">connect</button>
<div style="height: 0.3em;">&nbsp;</div>
<input id="message" type="text" placeholder="Type message" style="width: 20em;" />
<button id="send">send message</button>
<div style="margin-top: 1em;">Event log:</div>
<div id="log" style="background: #eee; height: 10em; padding: 0.5em;"><div>
</body>
<script>
var ws, E = function(id) { return document.getElementById(id); };
var url = E('url'), connect = E('connect'), message = E('message'), send = E('send'), log = E('log');
var enable = function(en) { message.disabled = send.disabled = !en; url.disabled = en; connect.innerHTML = en ? 'disconnect' : 'connect'; };
enable(false)
connect.onclick = function() {
if (ws) { ws.close(); return; }
ws = new WebSocket(url.value);
if (!ws) return;
ws.onopen = function() { log.innerHTML += 'CONNECTION OPENED<br/>'; }
ws.onmessage = function(ev) { log.innerHTML += 'RECEIVED: ' + ev.data + '<br/>'; }
ws.onerror = function(ev) { log.innerHTML += 'ERROR: ' + ev + '<br/>'; }
ws.onclose = function() { log.innerHTML += 'CONNECTION CLOSED<br/>'; enable(false); ws = null; }
enable(true);
};
send.onclick = function() {
if (!ws) return;
log.innerHTML += 'SENT: ' + message.value + '<br/>';
ws.send(message.value);
}
</script>
</html>