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