From 4d3a0375aabebc5ebc4132261baeeb061bce763d Mon Sep 17 00:00:00 2001 From: Eric Harding Date: Wed, 11 Apr 2018 18:11:50 -0400 Subject: [PATCH] Add websocket session tracking --- src/server/sparkd/http/WebServer.cpp | 44 ++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/src/server/sparkd/http/WebServer.cpp b/src/server/sparkd/http/WebServer.cpp index 6e11553cf..5886a206a 100644 --- a/src/server/sparkd/http/WebServer.cpp +++ b/src/server/sparkd/http/WebServer.cpp @@ -2,6 +2,7 @@ #include "lib/utils/Dispatcher.h" #include "lib/api/core/ApiClientInterop.h" +#include "lib/logging/logging.h" #include #include @@ -21,7 +22,6 @@ typedef std::string ApiKey; typedef websocketpp::server WsServer; using websocketpp::connection_hdl; -// using websocketpp::server; using websocketpp::lib::placeholders::_1; using websocketpp::lib::placeholders::_2; using websocketpp::lib::bind; @@ -37,16 +37,18 @@ std::string read_file(const std::string& path) { namespace bts { namespace http { - struct Client { + struct Session { std::mutex m; std::unordered_map apiclient; std::vector subscriptions; - Client(): - m(), apiclient(), subscriptions() + char count; + + Session(): + m(), apiclient(), subscriptions(), count('a') {} - ~Client() { + ~Session() { std::lock_guard lock(m); // unsubscribe // delete clients @@ -56,7 +58,7 @@ namespace http { struct ServerState { std::mutex _m; utils::Dispatcher& _dispatcher; - std::map _clients; + std::map, std::owner_less> _session; WsServer _server; int _port; std::string _root; @@ -65,7 +67,7 @@ namespace http { ServerState(const api::ServiceContext ctx, int port, const char* web_root) : _m(), _dispatcher(utils::Dispatcher::get_named("Webserver")), - _clients(), + _session(), _server(), _port(port), _root(web_root), @@ -82,21 +84,40 @@ namespace http { run(); } + // starts listening void run() { _server.listen(_port); _server.start_accept(); } void on_open(connection_hdl hdl) { - (void)hdl; + auto con = _server.get_con_from_hdl(hdl); + if (con) { + LOG(Api,Info, "Web client connection from {}", con->get_remote_endpoint()); + std::lock_guard lock(_m); + _session[hdl] = std::make_unique(); + + } } + void on_close(connection_hdl hdl) { - (void)hdl; + auto con = _server.get_con_from_hdl(hdl); + if (con) { + LOG(Api,Info, "Web client disconnected: {}", con->get_remote_endpoint()); + std::lock_guard lock(_m); + _session.erase(hdl); + } } + void on_message(connection_hdl hdl, WsServer::message_ptr msg) { - (void)hdl; - (void)msg; + std::lock_guard lock(_m); + auto iter = _session.find(hdl); + if (iter != _session.end()) { + LOG(Api,Info, "Got: {}", msg->get_payload()); + _server.send(hdl, "Your message has been received: " , websocketpp::frame::opcode::text); + } } + void on_http(connection_hdl hdl) { auto con = _server.get_con_from_hdl(hdl); auto resource = con->get_resource(); @@ -163,7 +184,6 @@ namespace http { } else if (boost::filesystem::is_regular_file(path)) { con->set_body(read_file(path.string())); - con->set_status(websocketpp::http::status_code::ok); } else {