Created
March 27, 2014 12:20
-
-
Save petermd/9806313 to your computer and use it in GitHub Desktop.
getting remote ip from ServerWebSocket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static | |
InetSocketAddress getRemoteAddress (final ServerWebSocket ws) | |
throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException | |
{ | |
InetSocketAddress addr = null; | |
final WebSocketImplBase requestWS = (WebSocketImplBase)ws; | |
Field f = WebSocketImplBase.class.getDeclaredField("conn"); //NoSuchFieldException | |
f.setAccessible(true); | |
ConnectionBase connectionBase = (ConnectionBase) f.get(requestWS); //IllegalAccessException | |
f = ConnectionBase.class.getDeclaredField("channel"); //NoSuchFieldException | |
f.setAccessible(true); | |
NioSocketChannel channel = (NioSocketChannel) f.get(connectionBase); //IllegalAccessException | |
if (channel != null) { | |
addr = channel.remoteAddress(); | |
} | |
return addr; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment