Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_remote_ip method to websocket::connection #263

Merged
merged 1 commit into from
Nov 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/websocket/example_ws.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::mutex> _(mtx);
users.insert(&conn);
})
Expand Down
6 changes: 6 additions & 0 deletions include/crow/websocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -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; }
Expand Down Expand Up @@ -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).
Expand Down