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

How can I get the IP address of the WebSocket client by crow::websocket::connection? #261

Closed
Jasonzyt opened this issue Oct 31, 2021 · 5 comments · Fixed by #263
Closed
Labels
question Issue can be closed by providing information

Comments

@Jasonzyt
Copy link

I want to print the IP address of the WebSocket client when it connects or disconnects to my server
image

@The-EDev The-EDev added the question Issue can be closed by providing information label Oct 31, 2021
@The-EDev
Copy link
Member

You can use onaccept (more info here), which provides the crow::request (ref) that was sent to establish the connection.

As for disconnecting, you'll need to keep a map of the connections and their related IP addresses.

@Jasonzyt
Copy link
Author

But i can't get crow::websocket::connection and make the IP and connection a pair

@The-EDev
Copy link
Member

you can store the connection from onopen in a temporary variable or stack until onaccept is called.

Another thing you can do is modify crow::websocket::connection to give you the IP address like so:

@@ -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).

@Jasonzyt
Copy link
Author

Jasonzyt commented Nov 6, 2021

Thanks very much!!!

@The-EDev
Copy link
Member

The-EDev commented Nov 6, 2021

No problem, thanks for making the suggestion :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Issue can be closed by providing information
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants