#include #include #include #include "websockets.h" int uid = 0; std::map people_online; // NOTE: we cannot use C++ ctors and dtors, because it will be allocated by libwebsockets (= C lib) struct User { unsigned int index; }; websockets::TestProtocol default_protocol{ // connection established [](User& user) -> int{ user.index = uid++; people_online[user.index] = ""; return 0; }, // connection closed [](User& user) -> int{ people_online.erase(user.index); return 0; }, // write (will always come after receive) [](User& user) -> std::string{ std::string string_to_send = "Other People:"; for(auto x : people_online) if(x.first != user.index) string_to_send += " " + x.second; return string_to_send; }, // receive [](User& user, std::string in) -> int{ people_online[user.index] = in; return 0; } }; static libwebsocket_protocols protocols[] = { WSstandard_protocol("default", default_protocol), { NULL, NULL, 0 } // end of list }; int main(int argc, char **argv){ return websockets::default_main(argc, argv, protocols); }