|
@ -1,6 +1,7 @@ |
|
|
|
|
|
|
|
|
#include <map> |
|
|
|
|
|
#include <string> |
|
|
#include <string> |
|
|
|
|
|
#include <vector> |
|
|
|
|
|
#include <algorithm> |
|
|
|
|
|
|
|
|
#include <lib/libwebsockets.h> |
|
|
#include <lib/libwebsockets.h> |
|
|
#include <json_spirit/json_spirit.h> |
|
|
#include <json_spirit/json_spirit.h> |
|
@ -43,8 +44,8 @@ struct User { |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
// unfortunately libwebsockets own the user thingy
|
|
|
// unfortunately libwebsockets owns the user thingy
|
|
|
std::map<unsigned int, User*> people_online; |
|
|
std::vector<User*> people_online; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
websockets::TestProtocol<User> default_protocol{ |
|
|
websockets::TestProtocol<User> default_protocol{ |
|
@ -52,16 +53,16 @@ websockets::TestProtocol<User> default_protocol{ |
|
|
[](User& user){ |
|
|
[](User& user){ |
|
|
user.name = "Unknown guest"; |
|
|
user.name = "Unknown guest"; |
|
|
user.index = uid++; |
|
|
user.index = uid++; |
|
|
people_online[user.index] = &user; |
|
|
people_online.push_back(&user); |
|
|
}, |
|
|
}, |
|
|
// connection closed
|
|
|
// connection closed
|
|
|
[](User& user){ |
|
|
[](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)
|
|
|
// write (will always come after receive)
|
|
|
[](User& user) -> std::string{ |
|
|
[](User& user) -> std::string{ |
|
|
std::string string_to_send = "Other People:"; |
|
|
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; |
|
|
return string_to_send; |
|
|
}, |
|
|
}, |
|
|
// receive
|
|
|
// receive
|
|
@ -87,16 +88,13 @@ websockets::TestProtocol<Empty> observer_protocol{ |
|
|
[](Empty& user) -> std::string{ |
|
|
[](Empty& user) -> std::string{ |
|
|
js::Array array; |
|
|
js::Array array; |
|
|
for(auto x : people_online){ |
|
|
for(auto x : people_online){ |
|
|
array.push_back(x.second->to_json()); |
|
|
array.push_back(x->to_json()); |
|
|
} |
|
|
} |
|
|
js::Value value(array); |
|
|
js::Value value(array); |
|
|
std::cout << "OUTPUT JSON: " << write(value, js::remove_trailing_zeros) << std::endl; |
|
|
|
|
|
return write(value); |
|
|
return write(value); |
|
|
}, |
|
|
}, |
|
|
// receive
|
|
|
// receive
|
|
|
[](Empty& user, std::string in){ |
|
|
[](Empty& user, std::string in){} |
|
|
std::cout << "INPUT: " << in << std::endl; |
|
|
|
|
|
} |
|
|
|
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
static libwebsocket_protocols protocols[] = { |
|
|
static libwebsocket_protocols protocols[] = { |
|
|