diff --git a/examples/websocket/example_ws.cpp b/examples/websocket/example_ws.cpp index 0543c0eec..7ad0d5d12 100644 --- a/examples/websocket/example_ws.cpp +++ b/examples/websocket/example_ws.cpp @@ -14,7 +14,7 @@ int main() CROW_ROUTE(app, "/ws") .websocket() .onopen([&](crow::websocket::connection& conn){ - CROW_LOG_INFO << "new websocket connection"; + CROW_LOG_INFO << "new websocket connection from " << conn.get_remote_ip(); std::lock_guard _(mtx); users.insert(&conn); }) diff --git a/include/crow/websocket.h b/include/crow/websocket.h index ece71b6d3..86120735f 100644 --- a/include/crow/websocket.h +++ b/include/crow/websocket.h @@ -26,6 +26,7 @@ namespace crow virtual void send_ping(const std::string& msg) = 0; virtual void send_pong(const std::string& msg) = 0; virtual void close(const std::string& msg = "quit") = 0; + virtual std::string get_remote_ip() = 0; virtual ~connection(){} void userdata(void* u) { userdata_ = u; } @@ -185,6 +186,11 @@ namespace crow }); } + std::string get_remote_ip() override + { + return adaptor_.remote_endpoint().address().to_string(); + } + protected: /// Generate the websocket headers using an opcode and the message size (in bytes).