Multi-user gravity beats for Sound of Science 2013 (server)
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.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 

67 lines
1.6 KiB

#include <memory>
#include <string>
#include <stdexcept>
#include <lib/libwebsockets.h>
#include "websockets.h"
#include "json.h"
#include "json_ext.h"
#include "basics.h"
#include "app.h"
std::unique_ptr<App> app;
using namespace websockets;
websockets::TestProtocol<User> default_protocol{
// connection established
[](User& user, basic_websocket_info){
app->login(user);
},
// connection closed
[](User& user, basic_websocket_info){
app->logout(user);
},
// write (will always come after receive)
app->update_positions();
return write_json(ptrvector_to_json(app->people_online));
[](User& user, basic_websocket_info) -> std::string{
},
// receive
[](User& user, std::string in, basic_websocket_info){
try{
user = from_json<IncomingPacket>(parse_json(in));
} catch(std::exception& e) {
throw websockets::runtime_error(e.what());
}
}
};
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)
app->update_positions();
return write_json(ptrvector_to_json(app->people_online));
[](Empty& user, basic_websocket_info) -> std::string{
},
// receive
[](Empty& user, std::string in, basic_websocket_info){}
};
int main(int argc, char **argv){
app.reset(new App);
libwebsocket_protocols protocols[] = {
WSstandard_protocol("default", default_protocol),
WSstandard_protocol("observer", observer_protocol),
{ NULL, NULL, 0 } // end of list
};
return websockets::default_main(argc, argv, protocols);
}