diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java index fdf2a17f2a..53db1c8d1e 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker00.java @@ -174,7 +174,9 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker { @Override public void operationComplete(ChannelFuture future) { ChannelPipeline p = future.channel().pipeline(); - p.replace(HttpRequestEncoder.class, "ws-encoder", new WebSocket00FrameEncoder()); + p.addAfter( + p.context(HttpRequestEncoder.class).name(), + "ws-encoder", new WebSocket00FrameEncoder()); } }); @@ -233,7 +235,9 @@ public class WebSocketClientHandshaker00 extends WebSocketClientHandshaker { setHandshakeComplete(); - channel.pipeline().get(HttpResponseDecoder.class).replace( + ChannelPipeline p = channel.pipeline(); + p.remove(HttpRequestEncoder.class); + p.get(HttpResponseDecoder.class).replace( "ws-decoder", new WebSocket00FrameDecoder(getMaxFramePayloadLength())); } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker08.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker08.java index 05bff21925..6f04080628 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker08.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker08.java @@ -153,7 +153,9 @@ public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker { @Override public void operationComplete(ChannelFuture future) { ChannelPipeline p = future.channel().pipeline(); - p.replace(HttpRequestEncoder.class, "ws-encoder", new WebSocket08FrameEncoder(true)); + p.addAfter( + p.context(HttpRequestEncoder.class).name(), + "ws-encoder", new WebSocket08FrameEncoder(true)); } }); @@ -210,7 +212,9 @@ public class WebSocketClientHandshaker08 extends WebSocketClientHandshaker { setHandshakeComplete(); - channel.pipeline().get(HttpResponseDecoder.class).replace( + ChannelPipeline p = channel.pipeline(); + p.remove(HttpRequestEncoder.class); + p.get(HttpResponseDecoder.class).replace( "ws-decoder", new WebSocket08FrameDecoder(false, allowExtensions, getMaxFramePayloadLength())); } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker13.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker13.java index a5a3668eaa..90b05893fd 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker13.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketClientHandshaker13.java @@ -153,7 +153,9 @@ public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker { @Override public void operationComplete(ChannelFuture future) { ChannelPipeline p = future.channel().pipeline(); - p.replace(HttpRequestEncoder.class, "ws-encoder", new WebSocket13FrameEncoder(true)); + p.addAfter( + p.context(HttpRequestEncoder.class).name(), + "ws-encoder", new WebSocket13FrameEncoder(true)); } }); @@ -210,7 +212,9 @@ public class WebSocketClientHandshaker13 extends WebSocketClientHandshaker { setHandshakeComplete(); - channel.pipeline().get(HttpResponseDecoder.class).replace( + ChannelPipeline p = channel.pipeline(); + p.remove(HttpRequestEncoder.class); + p.get(HttpResponseDecoder.class).replace( "ws-decoder", new WebSocket13FrameDecoder(false, allowExtensions, getMaxFramePayloadLength())); } diff --git a/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClient.java b/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClient.java index 34e28e8617..279345d670 100644 --- a/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClient.java +++ b/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClient.java @@ -49,7 +49,6 @@ import io.netty.handler.codec.http.HttpResponseDecoder; import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame; import io.netty.handler.codec.http.websocketx.PingWebSocketFrame; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; -import io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker; import io.netty.handler.codec.http.websocketx.WebSocketClientHandshakerFactory; import io.netty.handler.codec.http.websocketx.WebSocketVersion; @@ -79,9 +78,10 @@ public class WebSocketClient { // Connect with V13 (RFC 6455 aka HyBi-17). You can change it to V08 or V00. // If you change it to V00, ping is not supported and remember to change // HttpResponseDecoder to WebSocketHttpResponseDecoder in the pipeline. - final WebSocketClientHandshaker handshaker = - new WebSocketClientHandshakerFactory().newHandshaker( - uri, WebSocketVersion.V13, null, false, customHeaders); + final WebSocketClientHandler handler = + new WebSocketClientHandler( + new WebSocketClientHandshakerFactory().newHandshaker( + uri, WebSocketVersion.V13, null, false, customHeaders)); b.group(new NioEventLoopGroup()) .channel(NioSocketChannel.class) @@ -92,17 +92,17 @@ public class WebSocketClient { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast("decoder", new HttpResponseDecoder()); pipeline.addLast("encoder", new HttpRequestEncoder()); - pipeline.addLast("ws-handler", new WebSocketClientHandler(handshaker)); + pipeline.addLast("ws-handler", handler); } }); System.out.println("WebSocket Client connecting"); Channel ch = b.connect().sync().channel(); - handshaker.handshake(ch).sync(); + handler.handshakeFuture().sync(); // Send 10 messages and wait for responses System.out.println("WebSocket Client sending message"); - for (int i = 0; i < 1000; i++) { + for (int i = 0; i < 1; i++) { ch.write(new TextWebSocketFrame("Message #" + i)); } diff --git a/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientHandler.java b/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientHandler.java index 1a2f78b5a4..b852d3941f 100644 --- a/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientHandler.java +++ b/example/src/main/java/io/netty/example/http/websocketx/client/WebSocketClientHandler.java @@ -38,6 +38,7 @@ package io.netty.example.http.websocketx.client; import io.netty.channel.Channel; +import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelInboundMessageHandlerAdapter; import io.netty.handler.codec.http.HttpResponse; @@ -51,11 +52,26 @@ import io.netty.util.CharsetUtil; public class WebSocketClientHandler extends ChannelInboundMessageHandlerAdapter { private final WebSocketClientHandshaker handshaker; + private ChannelFuture handshakeFuture; public WebSocketClientHandler(WebSocketClientHandshaker handshaker) { this.handshaker = handshaker; } + public ChannelFuture handshakeFuture() { + return handshakeFuture; + } + + @Override + public void beforeAdd(ChannelHandlerContext ctx) throws Exception { + handshakeFuture = ctx.newFuture(); + } + + @Override + public void channelActive(ChannelHandlerContext ctx) throws Exception { + handshaker.handshake(ctx.channel()); + } + @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { System.out.println("WebSocket Client disconnected!"); @@ -67,6 +83,7 @@ public class WebSocketClientHandler extends ChannelInboundMessageHandlerAdapter< if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ch, (HttpResponse) msg); System.out.println("WebSocket Client connected!"); + handshakeFuture.setSuccess(); return; } @@ -91,6 +108,11 @@ public class WebSocketClientHandler extends ChannelInboundMessageHandlerAdapter< @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { cause.printStackTrace(); + + if (!handshakeFuture.isDone()) { + handshakeFuture.setFailure(cause); + } + ctx.close(); } }