|
|
@ -1,6 +1,7 @@ |
|
|
|
|
|
|
|
#include <map> |
|
|
|
#include <string> |
|
|
|
#include <vector> |
|
|
|
#include <algorithm> |
|
|
|
|
|
|
|
#include <lib/libwebsockets.h> |
|
|
|
#include <json_spirit/json_spirit.h> |
|
|
@ -43,8 +44,8 @@ struct User { |
|
|
|
} |
|
|
|
}; |
|
|
|
|
|
|
|
// unfortunately libwebsockets own the user thingy
|
|
|
|
std::map<unsigned int, User*> people_online; |
|
|
|
// unfortunately libwebsockets owns the user thingy
|
|
|
|
std::vector<User*> people_online; |
|
|
|
|
|
|
|
|
|
|
|
websockets::TestProtocol<User> default_protocol{ |
|
|
@ -52,16 +53,16 @@ websockets::TestProtocol<User> 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<Empty> 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[] = { |
|
|
|