#include "crow.h" #include #include int main() { crow::SimpleApp app; crow::mustache::set_base("."); std::mutex mtx;; std::unordered_set users; CROW_ROUTE(app, "/ws") .websocket() .onopen([&](crow::websocket::connection& conn){ CROW_LOG_INFO << "new websocket connection"; std::lock_guard _(mtx); users.insert(&conn); }) .onclose([&](crow::websocket::connection& conn, const std::string& reason){ CROW_LOG_INFO << "websocket connection closed: " << reason; std::lock_guard _(mtx); users.erase(&conn); }) .onmessage([&](crow::websocket::connection& /*conn*/, const std::string& data, bool is_binary){ std::lock_guard _(mtx); for(auto u:users) if (is_binary) u->send_binary(data); else u->send_text(data); }); CROW_ROUTE(app, "/") ([]{ return crow::mustache::load("ws.html").render(); }); app.port(40080) .multithreaded() .run(); }