diff --git a/jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketClient.java b/jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketClient.java index cff7de04304..cc5db438838 100644 --- a/jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketClient.java +++ b/jetty-websocket/src/main/java/org/eclipse/jetty/websocket/WebSocketClient.java @@ -332,6 +332,25 @@ public class WebSocketClient { if (!_factory.isStarted()) throw new IllegalStateException("Factory !started"); + + InetSocketAddress address = toSocketAddress(uri); + + SocketChannel channel = SocketChannel.open(); + if (_bindAddress != null) + channel.socket().bind(_bindAddress); + channel.socket().setTcpNoDelay(true); + + WebSocketFuture holder = new WebSocketFuture(websocket, uri, this, channel); + + channel.configureBlocking(false); + channel.connect(address); + _factory.getSelectorManager().register(channel, holder); + + return holder; + } + + public static final InetSocketAddress toSocketAddress(URI uri) + { String scheme = uri.getScheme(); if (!("ws".equalsIgnoreCase(scheme) || "wss".equalsIgnoreCase(scheme))) throw new IllegalArgumentException("Bad WebSocket scheme: " + scheme); @@ -341,20 +360,8 @@ public class WebSocketClient if (port < 0) port = "ws".equals(scheme) ? 80 : 443; - SocketChannel channel = SocketChannel.open(); - if (_bindAddress != null) - channel.socket().bind(_bindAddress); - channel.socket().setTcpNoDelay(true); - InetSocketAddress address = new InetSocketAddress(uri.getHost(), port); - - WebSocketFuture holder = new WebSocketFuture(websocket, uri, this, channel); - - channel.configureBlocking(false); - channel.connect(address); - _factory.getSelectorManager().register(channel, holder); - - return holder; + return address; } /* ------------------------------------------------------------ */ @@ -486,6 +493,7 @@ public class WebSocketClient return _maskGen; } + @Override public String toString() { return "[" + _uri + ","+_websocket+"]@"+hashCode(); diff --git a/jetty-websocket/src/test/java/org/eclipse/jetty/websocket/WebSocketClientTest.java b/jetty-websocket/src/test/java/org/eclipse/jetty/websocket/WebSocketClientTest.java index 77639bf0c65..2ab060ac239 100644 --- a/jetty-websocket/src/test/java/org/eclipse/jetty/websocket/WebSocketClientTest.java +++ b/jetty-websocket/src/test/java/org/eclipse/jetty/websocket/WebSocketClientTest.java @@ -15,12 +15,15 @@ *******************************************************************************/ package org.eclipse.jetty.websocket; +import static org.hamcrest.Matchers.*; + import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.ConnectException; +import java.net.InetSocketAddress; import java.net.ServerSocket; import java.net.Socket; import java.net.URI; @@ -43,8 +46,6 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import static org.hamcrest.Matchers.greaterThan; - public class WebSocketClientTest { private WebSocketClientFactory _factory = new WebSocketClientFactory(); @@ -711,44 +712,19 @@ public class WebSocketClientTest @Test public void testURIWithDefaultPort() throws Exception { - WebSocketClient client = new WebSocketClient(_factory); - - try - { - client.open(new URI("ws://localhost"), new WebSocket() - { - public void onOpen(Connection connection) - { - } - - public void onClose(int closeCode, String message) - { - System.out.println("closeCode = " + closeCode); - } - }).get(5, TimeUnit.SECONDS); - } - catch (ExecutionException x) - { - Assert.assertTrue(x.getCause() instanceof ConnectException); - } - - try - { - client.open(new URI("wss://localhost"), new WebSocket() - { - public void onOpen(Connection connection) - { - } - - public void onClose(int closeCode, String message) - { - } - }).get(5, TimeUnit.SECONDS); - } - catch (ExecutionException x) - { - Assert.assertTrue(x.getCause() instanceof ConnectException); - } + URI uri = new URI("ws://localhost"); + InetSocketAddress addr = WebSocketClient.toSocketAddress(uri); + Assert.assertThat("URI (" + uri + ").host", addr.getHostName(), is("localhost")); + Assert.assertThat("URI (" + uri + ").port", addr.getPort(), is(80)); + } + + @Test + public void testURIWithDefaultWSSPort() throws Exception + { + URI uri = new URI("wss://localhost"); + InetSocketAddress addr = WebSocketClient.toSocketAddress(uri); + Assert.assertThat("URI (" + uri + ").host", addr.getHostName(), is("localhost")); + Assert.assertThat("URI (" + uri + ").port", addr.getPort(), is(443)); } private void respondToClient(Socket connection, String serverResponse) throws IOException