Handle handshake failure in Websocket Client example

Motivation:

We need to fail the promise if a failure during handshake happens.

Modification:

Correctly fail the promise.

Result:

Correct websocket client example. Fixes [#6998]
This commit is contained in:
martin vseticka 2017-07-20 12:14:48 +02:00 committed by Norman Maurer
parent f23b2fc25d
commit f897507b09

View File

@ -48,6 +48,7 @@ import io.netty.handler.codec.http.websocketx.PongWebSocketFrame;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker; import io.netty.handler.codec.http.websocketx.WebSocketClientHandshaker;
import io.netty.handler.codec.http.websocketx.WebSocketFrame; import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketHandshakeException;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object> { public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object> {
@ -82,9 +83,14 @@ public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object>
public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
Channel ch = ctx.channel(); Channel ch = ctx.channel();
if (!handshaker.isHandshakeComplete()) { if (!handshaker.isHandshakeComplete()) {
handshaker.finishHandshake(ch, (FullHttpResponse) msg); try {
System.out.println("WebSocket Client connected!"); handshaker.finishHandshake(ch, (FullHttpResponse) msg);
handshakeFuture.setSuccess(); System.out.println("WebSocket Client connected!");
handshakeFuture.setSuccess();
} catch (WebSocketHandshakeException e) {
System.out.println("WebSocket Client failed to connect");
handshakeFuture.setFailure(e);
}
return; return;
} }