You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
48 lines
1.1 KiB
48 lines
1.1 KiB
|
|
#include <map>
|
|
#include <string>
|
|
|
|
#include <lib/libwebsockets.h>
|
|
#include "websockets.h"
|
|
|
|
int uid = 0;
|
|
std::map<int, std::string> 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<User> 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);
|
|
}
|
|
|