diff --git a/src/main/java/io/netty/example/http/websocketx/autobahn/WebSocketServer.java b/src/main/java/io/netty/example/http/websocketx/autobahn/WebSocketServer.java index a6648cd56a..25251152ad 100644 --- a/src/main/java/io/netty/example/http/websocketx/autobahn/WebSocketServer.java +++ b/src/main/java/io/netty/example/http/websocketx/autobahn/WebSocketServer.java @@ -47,6 +47,6 @@ public class WebSocketServer { // Bind and start to accept incoming connections. bootstrap.bind(new InetSocketAddress(9000)); - System.out.println("Web Socket Server started on 9000. Open your browser and navigate to http://localhost:9000/"); + System.out.println("Web Socket Server started on localhost:9000."); } } diff --git a/src/main/java/io/netty/example/http/websocketx/client/App.java b/src/main/java/io/netty/example/http/websocketx/client/App.java index ebba304630..2928923e6e 100644 --- a/src/main/java/io/netty/example/http/websocketx/client/App.java +++ b/src/main/java/io/netty/example/http/websocketx/client/App.java @@ -55,8 +55,9 @@ public class App { MyCallbackHandler callbackHandler = new MyCallbackHandler(); WebSocketClientFactory factory = new WebSocketClientFactory(); - // Connect with spec version 17 (try changing it to V10 or V00 and it will - // still work ... fingers crossed ;-) + // Connect with spec version 17. You can change it to V10 or V00. + // If you change it to V00, ping is not supported and remember to change HttpResponseDecoder to + // WebSocketHttpResponseDecoder in the pipeline. WebSocketClient client = factory.newClient(new URI("ws://localhost:8080/websocket"), WebSocketSpecificationVersion.V17, callbackHandler); @@ -72,7 +73,7 @@ public class App { } Thread.sleep(1000); - // Ping + // Ping - only supported for V10 and up. System.out.println("WebSocket Client sending ping"); client.send(new PingWebSocketFrame(ChannelBuffers.copiedBuffer(new byte[] { 1, 2, 3, 4, 5, 6 }))); Thread.sleep(1000); diff --git a/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientFactory.java b/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientFactory.java index f3984c3a9b..47e758e0ee 100644 --- a/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientFactory.java +++ b/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientFactory.java @@ -28,6 +28,7 @@ import io.netty.channel.ChannelPipelineFactory; import io.netty.channel.Channels; import io.netty.channel.socket.nio.NioClientSocketChannelFactory; import io.netty.handler.codec.http.HttpRequestEncoder; +import io.netty.handler.codec.http.HttpResponseDecoder; import io.netty.handler.codec.http.websocketx.WebSocketSpecificationVersion; import java.net.URI; @@ -45,7 +46,7 @@ public class WebSocketClientFactory { Executors.newCachedThreadPool(), Executors.newCachedThreadPool()); /** - * Create a new WebSocket client + * Create a new WebSocket client. * * @param url * URL to connect to. @@ -72,7 +73,11 @@ public class WebSocketClientFactory { @Override public ChannelPipeline getPipeline() throws Exception { ChannelPipeline pipeline = Channels.pipeline(); - pipeline.addLast("decoder", new WebSocketHttpResponseDecoder()); + + // If you wish to support HyBi V00, you need to use WebSocketHttpResponseDecoder instead for + // HttpResponseDecoder. + pipeline.addLast("decoder", new HttpResponseDecoder()); + pipeline.addLast("encoder", new HttpRequestEncoder()); pipeline.addLast("ws-handler", clientHandler); return pipeline; diff --git a/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientHandler.java b/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientHandler.java index 21863fa56d..3649911ace 100644 --- a/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientHandler.java +++ b/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientHandler.java @@ -66,7 +66,7 @@ public class WebSocketClientHandler extends SimpleChannelUpstreamHandler impleme public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception { channel = e.getChannel(); this.handshaker = new WebSocketClientHandshakerFactory().newHandshaker(url, version, null, false); - handshaker.performOpeningHandshake(ctx, channel); + handshaker.performOpeningHandshake(channel); } @Override @@ -77,7 +77,7 @@ public class WebSocketClientHandler extends SimpleChannelUpstreamHandler impleme @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception { if (!handshaker.isOpeningHandshakeCompleted()) { - handshaker.performClosingHandshake(ctx, (HttpResponse) e.getMessage()); + handshaker.performClosingHandshake(ctx.getChannel(), (HttpResponse) e.getMessage()); callback.onConnect(this); return; } diff --git a/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java b/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java index 3be4d65f0c..5e2eb22847 100644 --- a/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java +++ b/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker.java @@ -22,7 +22,6 @@ import java.security.NoSuchAlgorithmException; import io.netty.buffer.ChannelBuffer; import io.netty.buffer.ChannelBuffers; import io.netty.channel.Channel; -import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.base64.Base64; import io.netty.handler.codec.http.HttpResponse; import io.netty.util.CharsetUtil; @@ -114,22 +113,20 @@ public abstract class WebSocketClientHandshaker { /** * Performs the opening handshake * - * @param ctx - * Channel context * @param channel * Channel */ - public abstract void performOpeningHandshake(ChannelHandlerContext ctx, Channel channel); + public abstract void performOpeningHandshake(Channel channel); /** * Performs the closing handshake * - * @param ctx - * Channel context + * @param channel + * Channel * @param response * HTTP response containing the closing handshake details */ - public abstract void performClosingHandshake(ChannelHandlerContext ctx, HttpResponse response) throws WebSocketHandshakeException; + public abstract void performClosingHandshake(Channel channel, HttpResponse response) throws WebSocketHandshakeException; /** * Performs an MD5 hash diff --git a/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java b/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java index 8c9fdfa690..3abbf8c201 100644 --- a/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java +++ b/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java @@ -21,13 +21,14 @@ import java.util.Arrays; import io.netty.buffer.ChannelBuffers; import io.netty.channel.Channel; -import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.http.DefaultHttpRequest; import io.netty.handler.codec.http.HttpHeaders.Names; import io.netty.handler.codec.http.HttpHeaders.Values; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpRequest; +import io.netty.handler.codec.http.HttpRequestEncoder; import io.netty.handler.codec.http.HttpResponse; +import io.netty.handler.codec.http.HttpResponseDecoder; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpVersion; @@ -81,13 +82,11 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker { * ^n:ds[4U * * - * @param ctx - * Channel context * @param channel * Channel into which we can write our request */ @Override - public void performOpeningHandshake(ChannelHandlerContext ctx, Channel channel) { + public void performOpeningHandshake(Channel channel) { // Make keys int spaces1 = createRandomNumber(1, 12); int spaces2 = createRandomNumber(1, 12); @@ -147,7 +146,7 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker { channel.write(request); - ctx.getPipeline().replace("encoder", "ws-encoder", new WebSocket00FrameEncoder()); + channel.getPipeline().replace(HttpRequestEncoder.class, "ws-encoder", new WebSocket00FrameEncoder()); } /** @@ -166,15 +165,15 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker { * 8jKS'y:G*Co,Wxa- * * - * @param ctx - * Channel context + * @param channel + * Channel * @param response * HTTP response returned from the server for the request sent by * beginOpeningHandshake00(). * @throws WebSocketHandshakeException */ @Override - public void performClosingHandshake(ChannelHandlerContext ctx, HttpResponse response) throws WebSocketHandshakeException { + public void performClosingHandshake(Channel channel, HttpResponse response) throws WebSocketHandshakeException { final HttpResponseStatus status = new HttpResponseStatus(101, "WebSocket Protocol Handshake"); if (!response.getStatus().equals(status)) { @@ -199,7 +198,7 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker { String protocol = response.getHeader(Names.SEC_WEBSOCKET_PROTOCOL); this.setSubProtocolResponse(protocol); - ctx.getPipeline().replace("decoder", "ws-decoder", new WebSocket00FrameDecoder()); + channel.getPipeline().replace(HttpResponseDecoder.class, "ws-decoder", new WebSocket00FrameDecoder()); this.setOpenningHandshakeCompleted(true); } diff --git a/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker10.java b/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker10.java index 5eb868d1ba..1e6fdb1f48 100644 --- a/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker10.java +++ b/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker10.java @@ -18,13 +18,14 @@ package io.netty.handler.codec.http.websocketx; import java.net.URI; import io.netty.channel.Channel; -import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.http.DefaultHttpRequest; import io.netty.handler.codec.http.HttpHeaders.Names; import io.netty.handler.codec.http.HttpHeaders.Values; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpRequest; +import io.netty.handler.codec.http.HttpRequestEncoder; import io.netty.handler.codec.http.HttpResponse; +import io.netty.handler.codec.http.HttpResponseDecoder; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpVersion; import io.netty.logging.InternalLogger; @@ -90,13 +91,11 @@ public class WebSocketClientHandshaker10 extends WebSocketClientHandshaker { * Sec-WebSocket-Version: 8 * * - * @param ctx - * Channel context * @param channel * Channel into which we can write our request */ @Override - public void performOpeningHandshake(ChannelHandlerContext ctx, Channel channel) { + public void performOpeningHandshake(Channel channel) { // Get path URI wsURL = this.getWebSocketURL(); String path = wsURL.getPath(); @@ -130,7 +129,7 @@ public class WebSocketClientHandshaker10 extends WebSocketClientHandshaker { channel.write(request); - ctx.getPipeline().replace("encoder", "ws-encoder", new WebSocket08FrameEncoder(true)); + channel.getPipeline().replace(HttpRequestEncoder.class, "ws-encoder", new WebSocket08FrameEncoder(true)); } /** @@ -146,15 +145,15 @@ public class WebSocketClientHandshaker10 extends WebSocketClientHandshaker { * Sec-WebSocket-Protocol: chat * * - * @param ctx - * Channel context + * @param channel + * Channel * @param response * HTTP response returned from the server for the request sent by * beginOpeningHandshake00(). * @throws WebSocketHandshakeException */ @Override - public void performClosingHandshake(ChannelHandlerContext ctx, HttpResponse response) throws WebSocketHandshakeException { + public void performClosingHandshake(Channel channel, HttpResponse response) throws WebSocketHandshakeException { final HttpResponseStatus status = new HttpResponseStatus(101, "Switching Protocols"); if (!response.getStatus().equals(status)) { @@ -176,7 +175,7 @@ public class WebSocketClientHandshaker10 extends WebSocketClientHandshaker { throw new WebSocketHandshakeException(String.format("Invalid challenge. Actual: %s. Expected: %s", accept, this.expectedChallengeResponseString)); } - ctx.getPipeline().replace("decoder", "ws-decoder", new WebSocket08FrameDecoder(false, this.allowExtensions)); + channel.getPipeline().replace(HttpResponseDecoder.class, "ws-decoder", new WebSocket08FrameDecoder(false, this.allowExtensions)); this.setOpenningHandshakeCompleted(true); } diff --git a/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker17.java b/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker17.java index 55cf6006f0..262eb7d40c 100644 --- a/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker17.java +++ b/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker17.java @@ -18,13 +18,14 @@ package io.netty.handler.codec.http.websocketx; import java.net.URI; import io.netty.channel.Channel; -import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.http.DefaultHttpRequest; import io.netty.handler.codec.http.HttpHeaders.Names; import io.netty.handler.codec.http.HttpHeaders.Values; import io.netty.handler.codec.http.HttpMethod; import io.netty.handler.codec.http.HttpRequest; +import io.netty.handler.codec.http.HttpRequestEncoder; import io.netty.handler.codec.http.HttpResponse; +import io.netty.handler.codec.http.HttpResponseDecoder; import io.netty.handler.codec.http.HttpResponseStatus; import io.netty.handler.codec.http.HttpVersion; import io.netty.logging.InternalLogger; @@ -90,13 +91,11 @@ public class WebSocketClientHandshaker17 extends WebSocketClientHandshaker { * Sec-WebSocket-Version: 13 * * - * @param ctx - * Channel context * @param channel * Channel into which we can write our request */ @Override - public void performOpeningHandshake(ChannelHandlerContext ctx, Channel channel) { + public void performOpeningHandshake(Channel channel) { // Get path URI wsURL = this.getWebSocketURL(); String path = wsURL.getPath(); @@ -130,7 +129,7 @@ public class WebSocketClientHandshaker17 extends WebSocketClientHandshaker { channel.write(request); - ctx.getPipeline().replace("encoder", "ws-encoder", new WebSocket13FrameEncoder(true)); + channel.getPipeline().replace(HttpRequestEncoder.class, "ws-encoder", new WebSocket13FrameEncoder(true)); } /** @@ -146,15 +145,15 @@ public class WebSocketClientHandshaker17 extends WebSocketClientHandshaker { * Sec-WebSocket-Protocol: chat * * - * @param ctx - * Channel context + * @param channel + * Channel * @param response * HTTP response returned from the server for the request sent by * beginOpeningHandshake00(). * @throws WebSocketHandshakeException */ @Override - public void performClosingHandshake(ChannelHandlerContext ctx, HttpResponse response) throws WebSocketHandshakeException { + public void performClosingHandshake(Channel channel, HttpResponse response) throws WebSocketHandshakeException { final HttpResponseStatus status = new HttpResponseStatus(101, "Switching Protocols"); if (!response.getStatus().equals(status)) { @@ -176,7 +175,7 @@ public class WebSocketClientHandshaker17 extends WebSocketClientHandshaker { throw new WebSocketHandshakeException(String.format("Invalid challenge. Actual: %s. Expected: %s", accept, this.expectedChallengeResponseString)); } - ctx.getPipeline().replace("decoder", "ws-decoder", new WebSocket13FrameDecoder(false, this.allowExtensions)); + channel.getPipeline().replace(HttpResponseDecoder.class, "ws-decoder", new WebSocket13FrameDecoder(false, this.allowExtensions)); this.setOpenningHandshakeCompleted(true); }