2013-12-26 18:13:04 +08:00
|
|
|
#include "mongoose.h"
|
|
|
|
|
2013-12-26 18:20:02 +08:00
|
|
|
// Start a browser and hit refresh couple of times. The replies will
|
|
|
|
// come from both server instances.
|
2014-03-02 20:16:09 +08:00
|
|
|
static int ev_handler(struct mg_connection *conn, enum mg_event ev) {
|
2014-03-03 18:26:27 +08:00
|
|
|
if (ev == MG_REQUEST) {
|
2014-03-02 20:16:09 +08:00
|
|
|
mg_send_header(conn, "Content-Type", "text/plain");
|
|
|
|
mg_printf_data(conn, "This is a reply from server instance # %s",
|
|
|
|
(char *) conn->server_param);
|
|
|
|
return MG_TRUE;
|
|
|
|
} else if (ev == MG_AUTH) {
|
|
|
|
return MG_TRUE;
|
|
|
|
} else {
|
|
|
|
return MG_FALSE;
|
|
|
|
}
|
2013-12-26 18:13:04 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void *serve(void *server) {
|
|
|
|
for (;;) mg_poll_server((struct mg_server *) server, 1000);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(void) {
|
|
|
|
struct mg_server *server1, *server2;
|
|
|
|
|
2014-03-02 20:16:09 +08:00
|
|
|
server1 = mg_create_server((void *) "1", ev_handler);
|
|
|
|
server2 = mg_create_server((void *) "2", ev_handler);
|
2013-12-26 18:13:04 +08:00
|
|
|
|
2014-10-14 22:25:56 +08:00
|
|
|
// Make both server1 and server2 listen on the same sockets
|
2013-12-26 18:13:04 +08:00
|
|
|
mg_set_option(server1, "listening_port", "8080");
|
2014-10-14 22:25:56 +08:00
|
|
|
mg_copy_listeners(server1, server2);
|
2013-12-26 18:13:04 +08:00
|
|
|
|
2013-12-31 17:06:21 +08:00
|
|
|
// server1 goes to separate thread, server 2 runs in main thread.
|
|
|
|
// IMPORTANT: NEVER LET DIFFERENT THREADS HANDLE THE SAME SERVER.
|
2013-12-26 18:13:04 +08:00
|
|
|
mg_start_thread(serve, server1);
|
2014-01-07 17:24:48 +08:00
|
|
|
mg_start_thread(serve, server2);
|
|
|
|
getchar();
|
2013-12-26 18:13:04 +08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|