#pragma once #include #include #include #include typedef websocketpp::server Server; using websocketpp::lib::placeholders::_1; using websocketpp::lib::placeholders::_2; /* on_open insert connection_hdl into channel * on_close remove connection_hdl from channel * on_message queue send to all channels */ enum action_type { SUBSCRIBE, UNSUBSCRIBE, MESSAGE }; struct action { action(action_type t, websocketpp::connection_hdl h) : type(t), hdl(h) {} action(action_type t, websocketpp::connection_hdl h, Server::message_ptr m) : type(t), hdl(h), msg(m) {} action_type type; websocketpp::connection_hdl hdl; Server::message_ptr msg; }; class Websocket { public: Websocket(uint16_t port, std::function on_message, std::function on_update); void run(); void stop(); void process_messages(); void process_broadcast(); private: void on_open(websocketpp::connection_hdl hdl); void on_close(websocketpp::connection_hdl hdl); void on_message(websocketpp::connection_hdl hdl, Server::message_ptr msg); private: std::function m_on_message; std::function m_on_update; Server m_server; uint16_t m_port; typedef std::set> con_list; con_list m_connections; std::queue m_actions; std::mutex m_action_lock; std::mutex m_connection_lock; std::condition_variable m_action_cond; };