From 85e85c3e641dcccee664c9f8e8dc3a6437203587 Mon Sep 17 00:00:00 2001 From: The-EDev Date: Mon, 1 Nov 2021 23:57:21 +0300 Subject: [PATCH] allow remote IP to be accessed from websocket object --- examples/websocket/example_ws.cpp | 2 +- include/crow/websocket.h | 6 ++++++ 2 files changed, 7 insertions(+), 1 deletion(-) 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).