|
@ -31,24 +31,35 @@ websockets::TestProtocol<User> default_protocol{ |
|
|
[](User& user, basic_websocket_info) -> std::string{ |
|
|
[](User& user, basic_websocket_info) -> std::string{ |
|
|
js::Object ret; |
|
|
js::Object ret; |
|
|
if(user.update_lines){ |
|
|
if(user.update_lines){ |
|
|
ret["lines"] = vector_to_json(app->lines, [](AbstractLine const & l){return cheap_line_type(l);}); |
|
|
if(app->uberclient){ |
|
|
|
|
|
ret["lines"] = vector_to_json(app->offline_sim.lines); |
|
|
|
|
|
} else { |
|
|
|
|
|
ret["lines"] = vector_to_json(app->online_sim.lines, [](AbstractLine const & l){return cheap_line_type(l);}); |
|
|
|
|
|
} |
|
|
user.update_lines = false; |
|
|
user.update_lines = false; |
|
|
} else { |
|
|
} else { |
|
|
ret["balls"] = vector_to_json(app->sim.balls, [](ball_type const & b){return cheap_ball_type(b);}); |
|
|
if(app->uberclient){ |
|
|
|
|
|
ret["balls"] = vector_to_json(app->offline_sim.balls); |
|
|
|
|
|
} else { |
|
|
|
|
|
ret["balls"] = vector_to_json(app->online_sim.sim.balls, [](ball_type const & b){return cheap_ball_type(b);}); |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
return write_json(ret); |
|
|
return write_json(ret); |
|
|
}, |
|
|
}, |
|
|
// receive
|
|
|
// receive
|
|
|
[](User& user, std::string in, basic_websocket_info){ |
|
|
[](User& user, std::string in, basic_websocket_info binfo){ |
|
|
try{ |
|
|
try{ |
|
|
auto object = parse_json(in).getObject(); |
|
|
auto object = parse_json(in).getObject(); |
|
|
auto command = object["command"]; |
|
|
auto command = object["command"]; |
|
|
if(command == "add line"){ |
|
|
if(command == "add line"){ |
|
|
app->add_line(from_json<AbstractLine>(object["data"])); |
|
|
app->add_line(from_json<cheap_line_type>(object["data"])); |
|
|
std::cout << "Line from: " << &user << ": " << user.name << std::endl; |
|
|
std::cout << "Line from: " << &user << ": " << user.name << std::endl; |
|
|
|
|
|
request_write(binfo); |
|
|
} else if (command == "update user") { |
|
|
} else if (command == "update user") { |
|
|
user = from_json<IncomingPacket>(object["data"]); |
|
|
user = from_json<IncomingPacket>(object["data"]); |
|
|
std::cout << "Updated user: " << &user << ": " << user.name << std::endl; |
|
|
std::cout << "Updated user: " << &user << ": " << user.name << std::endl; |
|
|
|
|
|
} else if (command == "poll") { |
|
|
|
|
|
request_write(binfo); |
|
|
} |
|
|
} |
|
|
} catch(std::exception& e) { |
|
|
} catch(std::exception& e) { |
|
|
throw websockets::runtime_error(e.what()); |
|
|
throw websockets::runtime_error(e.what()); |
|
@ -56,32 +67,16 @@ websockets::TestProtocol<User> default_protocol{ |
|
|
} |
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
websockets::TestProtocol<Empty> observer_protocol{ |
|
|
|
|
|
// connection established
|
|
|
|
|
|
[](Empty& user, basic_websocket_info){}, |
|
|
|
|
|
// connection closed
|
|
|
|
|
|
[](Empty& user, basic_websocket_info){}, |
|
|
|
|
|
// write (will always come after receive)
|
|
|
|
|
|
[](Empty& user, basic_websocket_info) -> std::string{ |
|
|
|
|
|
js::Object ret; |
|
|
|
|
|
ret["people"] = vector_to_json(app->people_online, [](User* u){return *u;}); |
|
|
|
|
|
ret["balls"] = vector_to_json(app->sim.balls); |
|
|
|
|
|
ret["lines"] = vector_to_json(app->sim.lines); |
|
|
|
|
|
return write_json(ret); |
|
|
|
|
|
}, |
|
|
|
|
|
// receive
|
|
|
|
|
|
[](Empty& user, std::string in, basic_websocket_info){} |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
websockets::TestProtocol<Empty> uberclient_protocol{ |
|
|
websockets::TestProtocol<Empty> uberclient_protocol{ |
|
|
// connection established
|
|
|
// connection established
|
|
|
[](Empty& user, basic_websocket_info binfo){ |
|
|
[](Empty& user, basic_websocket_info binfo){ |
|
|
if(app->uberclient) throw websockets::runtime_error("Warning: another uberclient connection\n"); |
|
|
std::cout << "uberclient connected!\n"; |
|
|
app->uberclient = binfo.wsi; |
|
|
app->uberclient_connected(binfo); |
|
|
}, |
|
|
}, |
|
|
// connection closed
|
|
|
// connection closed
|
|
|
[](Empty& user, basic_websocket_info){ |
|
|
[](Empty& user, basic_websocket_info binfo){ |
|
|
app->uberclient = nullptr; |
|
|
std::cout << "uberclient left!\n"; |
|
|
|
|
|
app->uberclient_disconnected(binfo); |
|
|
}, |
|
|
}, |
|
|
// write (will always come after receive)
|
|
|
// write (will always come after receive)
|
|
|
[](Empty& user, basic_websocket_info) -> std::string{ |
|
|
[](Empty& user, basic_websocket_info) -> std::string{ |
|
@ -89,7 +84,23 @@ websockets::TestProtocol<Empty> uberclient_protocol{ |
|
|
return std::to_string(i++); |
|
|
return std::to_string(i++); |
|
|
}, |
|
|
}, |
|
|
// receive
|
|
|
// receive
|
|
|
[](Empty& user, std::string in, basic_websocket_info){} |
|
|
[](Empty& user, std::string in, basic_websocket_info){ |
|
|
|
|
|
auto object = parse_json(in).getObject(); |
|
|
|
|
|
if(object.count("lines")){ |
|
|
|
|
|
app->offline_sim.lines.clear(); |
|
|
|
|
|
auto lines = object["lines"]; |
|
|
|
|
|
for(auto&& js : lines.getArray()){ |
|
|
|
|
|
app->offline_sim.lines.push_back(from_json<cheap_line_type>(js)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
if(object.count("balls")){ |
|
|
|
|
|
app->offline_sim.balls.clear(); |
|
|
|
|
|
auto balls = object["balls"]; |
|
|
|
|
|
for(auto&& js : balls.getArray()){ |
|
|
|
|
|
app->offline_sim.balls.push_back(from_json<cheap_ball_type>(js)); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
int main(int argc, char **argv){ |
|
|
int main(int argc, char **argv){ |
|
@ -97,7 +108,6 @@ int main(int argc, char **argv){ |
|
|
|
|
|
|
|
|
libwebsocket_protocols protocols[] = { |
|
|
libwebsocket_protocols protocols[] = { |
|
|
WSstandard_protocol("default", default_protocol), |
|
|
WSstandard_protocol("default", default_protocol), |
|
|
WSstandard_protocol("observer", observer_protocol), |
|
|
|
|
|
WSstandard_protocol("uberclient", uberclient_protocol), |
|
|
WSstandard_protocol("uberclient", uberclient_protocol), |
|
|
{ NULL, NULL, 0 } // end of list
|
|
|
{ NULL, NULL, 0 } // end of list
|
|
|
}; |
|
|
}; |
|
|