Code: Select all
auto server = mg_create_server(this, _server_handler, 0);
mg_set_option(server, "listening_port", std::to_string(port).c_str());
while (!_stop_server /* атомарная переменная */) {
mg_poll_server(server, 500);
}
mg_destroy_server(&server);
< /code>
Der Handler sieht so aus: < /p>
static int _server_handler(mg_connection * conn, mg_event ev) {
auto owner = static_cast(conn->server_param);
switch (ev) {
case MG_REQUEST:
mg_send_status(conn, 200);
if (!std::strcmp(conn->uri, "/api/push"))
owner->_api_push(conn);
return MG_TRUE;
case MG_AUTH:
return MG_TRUE;
default:
return MG_FALSE;
}
}