简体   繁体   中英

How to get the local ip address from a Tyrus websocket client

I'm writing a Java websocket client that needs to be aware of its own IP address, meaning the IP address of the interface used to connect to the server endpoint .

Since clients might change addresses, whenever they (re)connect to the server the local address has to be updated. This is easily achieved using Jetty's client since its Session class has a getLocalAddress() method.

Tyrus's session class on the other hand lack this ability . A simple workaround for this would be using a plain old socket. Whenever the client's onOpen in invoked, I just open a regular TCP socket to the server, get the socket's local address, then close it. But that's wasteful and error prone so I'd much rather dig into Tyrus and get the local address from its socket. Is there a way of doing that?

Tyrus is an implementation of JSR356 ( javax.websocket ), and as such, this level of information is not available.

Jetty pre-dates JSR356 and Tyrus by about 3 years and has learned alot about what people need from WebSocket.

You'll have to wait till version 2.0 of javax.websocket and pray it adds this level of information before you can hope to use it.

Note: there's currently no plans or efforts underway for javax.websocket 2.0, the expert-group at the JSR has been inactive for over a year now.

It is highly unlikely that you will be able to get this information from Tyrus, as it relies on javax.servlet.http.HttpServletRequest.upgrade() which uses a javax.servlet.http.HttpUpgradeHandler , which is handed a javax.servlet.http.WebConnection by the container. Neither of these two objects have the connection information exposed.

The Tyrus implementation of javax.servlet.http.HttpUpgradeHandler does not even track this information separately to include in the JSR356 layer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM