Files
mongoose/docs/tutorial.adoc
T
Marko Mikulicic f443c64341 Temporarily disable appdash from docker-compose
Until I read the doc and find how to limit the retention,
otherwise it just eats all my ram and cpu and things start to fall
apart.

PUBLISHED_FROM=eb33fb44736f07b992270689217aca4af70513ff
2016-05-15 22:07:04 +03:00

50 lines
1.2 KiB
Plaintext

== Usage Example
1. Copy `mongoose.c` and `mongoose.h` to your build tree
2. Write code that uses Mongoose API, e.g. in `my_app.c`
3. Compile application: `$ cc my_app.c mongoose.c`
[source,c]
.my_app.c
----
#include "mongoose.h" // <1>
static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { // <2>
struct mbuf *io = &nc->recv_mbuf;
switch (ev) {
case MG_EV_RECV:
// This event handler implements simple TCP echo server
mg_send(nc, io->buf, io->len); // Echo received data back
mbuf_remove(io, io->len); // Discard data from recv buffer
break;
default:
break;
}
}
int main(void) {
struct mg_mgr mgr;
mg_mgr_init(&mgr, NULL); // <3>
// Note that many connections can be added to a single event manager
// Connections can be created at any point, e.g. in event handler function
mg_bind(&mgr, "1234", ev_handler); // <4>
for (;;) { // <5>
mg_mgr_poll(&mgr, 1000);
}
mg_mgr_free(&mgr);
return 0;
}
----
<1> Include Mongoose API definitions
<2> Define an event handler function
<3> Initialize event manager object
<4> Create listening connection and add it to the event manager
<5> Start infinite event loop