mirror of
https://github.com/cesanta/mongoose.git
synced 2026-10-15 10:29:47 +00:00
start moving to new skeleton
This commit is contained in:
1 parent
71a48ccde0
commit
5345c7260e
20 files changed
+14
-6
No files matched your search
@@ -0,0 +1 @@
|
||||
See detailed tutorials at https://mongoose.ws/documentation/#core
|
||||
@@ -0,0 +1,25 @@
|
||||
PROG ?= example # Program we are building
|
||||
DELETE = rm -rf # Command to remove files
|
||||
OUT ?= -o $(PROG) # Compiler argument for output file
|
||||
SOURCES = main.c mongoose.c # Source code files
|
||||
CFLAGS = -W -Wall -Wextra -g -I. # Build options
|
||||
|
||||
# Mongoose build options. See https://mongoose.ws/documentation/#build-options
|
||||
CFLAGS_MONGOOSE += -DMG_ENABLE_LINES
|
||||
|
||||
ifeq ($(OS),Windows_NT) # Windows settings. Assume MinGW compiler. To use VC: make CC=cl CFLAGS=/MD OUT=/Feprog.exe
|
||||
PROG ?= example.exe # Use .exe suffix for the binary
|
||||
CC = gcc # Use MinGW gcc compiler
|
||||
CFLAGS += -lws2_32 # Link against Winsock library
|
||||
DELETE = cmd /C del /Q /F /S # Command prompt command to delete files
|
||||
OUT ?= -o $(PROG) # Build output
|
||||
endif
|
||||
|
||||
all: $(PROG) # Default target. Build and run program
|
||||
$(RUN) ./$(PROG) $(ARGS)
|
||||
|
||||
$(PROG): $(SOURCES) # Build program from sources
|
||||
$(CC) $(SOURCES) $(CFLAGS) $(CFLAGS_MONGOOSE) $(CFLAGS_EXTRA) $(OUT)
|
||||
|
||||
clean: # Cleanup. Delete built program and all build artifacts
|
||||
$(DELETE) $(PROG) *.o *.obj *.exe *.dSYM
|
||||
@@ -0,0 +1 @@
|
||||
See detailed tutorial at https://mongoose.ws/tutorials/core/timers/
|
||||
@@ -0,0 +1,52 @@
|
||||
// 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";
|
||||
|
||||
// This RESTful server implements the following endpoints:
|
||||
static void fn(struct mg_connection *c, int ev, void *ev_data) {
|
||||
if (ev == MG_EV_HTTP_MSG) {
|
||||
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
|
||||
if (mg_match(hm->uri, mg_str("/websocket"), NULL)) {
|
||||
mg_ws_upgrade(c, hm, NULL); // Upgrade HTTP to Websocket
|
||||
c->data[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;
|
||||
mg_ws_send(c, wm->data.buf, wm->data.len, WEBSOCKET_OP_TEXT);
|
||||
mg_iobuf_del(&c->recv, 0, c->recv.len);
|
||||
}
|
||||
}
|
||||
|
||||
static void timer_fn(void *arg) {
|
||||
struct mg_mgr *mgr = (struct mg_mgr *) arg;
|
||||
// Broadcast "hi" message to all connected websocket clients.
|
||||
// Traverse over all connections
|
||||
for (struct mg_connection *c = mgr->conns; c != NULL; c = c->next) {
|
||||
// Send only to marked connections
|
||||
if (c->data[0] == 'W') mg_ws_send(c, "hi", 2, WEBSOCKET_OP_TEXT);
|
||||
}
|
||||
}
|
||||
|
||||
int main(void) {
|
||||
struct mg_mgr mgr; // Event manager
|
||||
mg_mgr_init(&mgr); // Initialise event manager
|
||||
mg_log_set(MG_LL_DEBUG); // Set log level
|
||||
mg_timer_add(&mgr, 1000, 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;
|
||||
}
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../../../mongoose.c
|
||||
Symlink
+1
@@ -0,0 +1 @@
|
||||
../../../mongoose.h
|
||||
@@ -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;"> </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>
|
||||
@@ -0,0 +1 @@
|
||||
See detailed tutorials at https://mongoose.ws/documentation/#http
|
||||
@@ -0,0 +1 @@
|
||||
See detailed tutorials at https://mongoose.ws/documentation/#mqtt
|
||||
@@ -0,0 +1 @@
|
||||
See detailed tutorials at https://mongoose.ws/documentation/#smtp
|
||||
@@ -0,0 +1 @@
|
||||
See detailed tutorials at https://mongoose.ws/documentation/#tcp
|
||||
@@ -0,0 +1 @@
|
||||
See detailed tutorials at https://mongoose.ws/documentation/#tcpip-stack
|
||||
@@ -0,0 +1 @@
|
||||
See detailed tutorials at https://mongoose.ws/documentation/#ssltls
|
||||
@@ -0,0 +1 @@
|
||||
See detailed tutorials at https://mongoose.ws/documentation/#udp
|
||||
@@ -0,0 +1 @@
|
||||
See detailed tutorials at https://mongoose.ws/documentation/#websocket
|
||||
@@ -0,0 +1 @@
|
||||
See detailed tutorials at https://mongoose.ws/documentation/#web-ui
|
||||
Reference in new issue
Block a user