|
28 | 28 | import java.io.IOException; |
29 | 29 | import java.io.InputStream; |
30 | 30 | import java.io.OutputStream; |
| 31 | +import java.net.InetAddress; |
31 | 32 | import java.net.InetSocketAddress; |
32 | 33 | import java.net.Proxy; |
33 | 34 | import java.net.Socket; |
34 | 35 | import java.net.URI; |
| 36 | +import java.net.UnknownHostException; |
35 | 37 | import java.nio.ByteBuffer; |
36 | 38 | import java.util.Collection; |
37 | 39 | import java.util.Collections; |
@@ -131,6 +133,14 @@ public abstract class WebSocketClient extends AbstractWebSocket implements Runna |
131 | 133 | */ |
132 | 134 | private int connectTimeout = 0; |
133 | 135 |
|
| 136 | + /** |
| 137 | + * DNS resolver that translates a URI to an InetAddress |
| 138 | + * |
| 139 | + * @see InetAddress |
| 140 | + * @since 1.4.1 |
| 141 | + */ |
| 142 | + private DnsResolver dnsResolver = null; |
| 143 | + |
134 | 144 | /** |
135 | 145 | * Constructs a WebSocketClient instance and sets it to the connect to the |
136 | 146 | * specified URI. The channel does not attampt to connect automatically. The connection |
@@ -195,6 +205,12 @@ public WebSocketClient( URI serverUri , Draft protocolDraft , Map<String,String> |
195 | 205 | } |
196 | 206 | this.uri = serverUri; |
197 | 207 | this.draft = protocolDraft; |
| 208 | + this.dnsResolver = new DnsResolver() { |
| 209 | + @Override |
| 210 | + public InetAddress resolve(URI uri) throws UnknownHostException { |
| 211 | + return InetAddress.getByName(uri.getHost()); |
| 212 | + } |
| 213 | + }; |
198 | 214 | if(httpHeaders != null) { |
199 | 215 | headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER); |
200 | 216 | headers.putAll(httpHeaders); |
@@ -266,6 +282,17 @@ public void clearHeaders() { |
266 | 282 | headers = null; |
267 | 283 | } |
268 | 284 |
|
| 285 | + /** |
| 286 | + * Sets a custom DNS resolver. |
| 287 | + * |
| 288 | + * @param dnsResolver The DnsResolver to use. |
| 289 | + * |
| 290 | + * @since 1.4.1 |
| 291 | + */ |
| 292 | + public void setDnsResolver(DnsResolver dnsResolver) { |
| 293 | + this.dnsResolver = dnsResolver; |
| 294 | + } |
| 295 | + |
269 | 296 | /** |
270 | 297 | * Reinitiates the websocket connection. This method does not block. |
271 | 298 | * @since 1.3.8 |
@@ -431,8 +458,9 @@ public void run() { |
431 | 458 | socket.setTcpNoDelay( isTcpNoDelay() ); |
432 | 459 | socket.setReuseAddress( isReuseAddr() ); |
433 | 460 |
|
434 | | - if( !socket.isBound() ) { |
435 | | - socket.connect( new InetSocketAddress( uri.getHost(), getPort() ), connectTimeout ); |
| 461 | + if (!socket.isBound()) { |
| 462 | + InetSocketAddress addr = new InetSocketAddress(dnsResolver.resolve(uri), this.getPort()); |
| 463 | + socket.connect(addr, connectTimeout); |
436 | 464 | } |
437 | 465 |
|
438 | 466 | // if the socket is set by others we don't apply any TLS wrapper |
@@ -509,9 +537,9 @@ private void sendHandshake() throws InvalidHandshakeException { |
509 | 537 | if( part2 != null ) |
510 | 538 | path += '?' + part2; |
511 | 539 | int port = getPort(); |
512 | | - String host = uri.getHost() + ( |
| 540 | + String host = uri.getHost() + ( |
513 | 541 | (port != WebSocketImpl.DEFAULT_PORT && port != WebSocketImpl.DEFAULT_WSS_PORT) |
514 | | - ? ":" + port |
| 542 | + ? ":" + port |
515 | 543 | : "" ); |
516 | 544 |
|
517 | 545 | HandshakeImpl1Client handshake = new HandshakeImpl1Client(); |
@@ -844,7 +872,7 @@ public InetSocketAddress getLocalSocketAddress() { |
844 | 872 | public InetSocketAddress getRemoteSocketAddress() { |
845 | 873 | return engine.getRemoteSocketAddress(); |
846 | 874 | } |
847 | | - |
| 875 | + |
848 | 876 | @Override |
849 | 877 | public String getResourceDescriptor() { |
850 | 878 | return uri.getPath(); |
|
0 commit comments