diff --git a/main.cpp b/main.cpp index 9da9132..b7f7791 100644 --- a/main.cpp +++ b/main.cpp @@ -1,6 +1,7 @@ -#include #include +#include +#include #include #include @@ -43,8 +44,8 @@ struct User { } }; -// unfortunately libwebsockets own the user thingy -std::map people_online; +// unfortunately libwebsockets owns the user thingy +std::vector people_online; websockets::TestProtocol default_protocol{ @@ -52,16 +53,16 @@ websockets::TestProtocol default_protocol{ [](User& user){ user.name = "Unknown guest"; user.index = uid++; - people_online[user.index] = &user; + people_online.push_back(&user); }, // connection closed [](User& user){ - people_online.erase(user.index); + people_online.erase(std::remove(people_online.begin(), people_online.end(), &user), people_online.end()); }, // write (will always come after receive) [](User& user) -> std::string{ std::string string_to_send = "Other People:"; - for(auto x : people_online) if(x.second != &user) string_to_send += " " + x.second->name; + for(auto x : people_online) if(x != &user) string_to_send += " " + x->name; return string_to_send; }, // receive @@ -87,16 +88,13 @@ websockets::TestProtocol observer_protocol{ [](Empty& user) -> std::string{ js::Array array; for(auto x : people_online){ - array.push_back(x.second->to_json()); + array.push_back(x->to_json()); } js::Value value(array); - std::cout << "OUTPUT JSON: " << write(value, js::remove_trailing_zeros) << std::endl; return write(value); }, // receive - [](Empty& user, std::string in){ - std::cout << "INPUT: " << in << std::endl; - } + [](Empty& user, std::string in){} }; static libwebsocket_protocols protocols[] = {