allow remote IP to be accessed from websocket object

This commit is contained in:
The-EDev 2021-11-01 23:57:21 +03:00
parent 8f26cea2a6
commit 85e85c3e64
No known key found for this signature in database
GPG Key ID: 51C45DC0C413DCD9
2 changed files with 7 additions and 1 deletions

View File

@ -14,7 +14,7 @@ int main()
CROW_ROUTE(app, "/ws") CROW_ROUTE(app, "/ws")
.websocket() .websocket()
.onopen([&](crow::websocket::connection& conn){ .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<std::mutex> _(mtx); std::lock_guard<std::mutex> _(mtx);
users.insert(&conn); users.insert(&conn);
}) })

View File

@ -26,6 +26,7 @@ namespace crow
virtual void send_ping(const std::string& msg) = 0; virtual void send_ping(const std::string& msg) = 0;
virtual void send_pong(const std::string& msg) = 0; virtual void send_pong(const std::string& msg) = 0;
virtual void close(const std::string& msg = "quit") = 0; virtual void close(const std::string& msg = "quit") = 0;
virtual std::string get_remote_ip() = 0;
virtual ~connection(){} virtual ~connection(){}
void userdata(void* u) { userdata_ = u; } 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: protected:
/// Generate the websocket headers using an opcode and the message size (in bytes). /// Generate the websocket headers using an opcode and the message size (in bytes).