#include #include #include #include #include "websockets.h" #include "json.h" #include "json_ext.h" #include "basics.h" #include "app.h" std::unique_ptr app; using namespace websockets; websockets::TestProtocol default_protocol{ // connection established [](User& user, basic_websocket_info){ app->login(user); }, // connection closed [](User& user, basic_websocket_info){ app->logout(user); }, // write (will always come after receive) app->update_positions(); return write_json(ptrvector_to_json(app->people_online)); [](User& user, basic_websocket_info) -> std::string{ }, // receive [](User& user, std::string in, basic_websocket_info){ try{ user = from_json(parse_json(in)); } catch(std::exception& e) { throw websockets::runtime_error(e.what()); } } }; websockets::TestProtocol observer_protocol{ // connection established [](Empty& user, basic_websocket_info){}, // connection closed [](Empty& user, basic_websocket_info){}, // write (will always come after receive) app->update_positions(); return write_json(ptrvector_to_json(app->people_online)); [](Empty& user, basic_websocket_info) -> std::string{ }, // receive [](Empty& user, std::string in, basic_websocket_info){} }; int main(int argc, char **argv){ app.reset(new App); libwebsocket_protocols protocols[] = { WSstandard_protocol("default", default_protocol), WSstandard_protocol("observer", observer_protocol), { NULL, NULL, 0 } // end of list }; return websockets::default_main(argc, argv, protocols); }