Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] How to get remote IP address as String in WebSocketListener?

The Session is the correct place to start.

Using Session.getRemoteAddress() returns a java.net.InetSocketAddress which is the appropriate thing to return.

After that, you are in the world of Java networking, and all that it implies.
Note that the InetSocketAddress can be an IPv4 or IPv6 address.

Testing for (session != null) is appropriate in many places.  
But seeing as you didn't  specify where you are doing this, its hard to be more precise.

If you decide to pull the remote address during onWebSocketConnect() then its safe to skip the session != null check.

(hmm, not sure what would happen if the websocket was extremely short lived)


Joakim Erdfelt / joakim@xxxxxxxxxxx

On Tue, Jun 14, 2016 at 6:18 AM, Alexander Farber <alexander.farber@xxxxxxxxx> wrote:
Hello, would calling

    String ip = mSession.getRemoteAddress().getAddress().getHostAddress();

be a good way of getting the remote IP address in the onWebSocketText callback of a custom WebSocketListener implementation?

I am asking because -

1) Is calling 3 methods necessary or maybe there is a shortcut?

2) Should (mSession != null) be checked first as in the example

http://www.eclipse.org/jetty/documentation/current/jetty-websocket-api-listener.html   ?

Thank you
Alex



_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit
https://dev.eclipse.org/mailman/listinfo/jetty-users


Back to the top