From f897507b09d61baca231f43bfc701c134966bd11 Mon Sep 17 00:00:00 2001 From: martin vseticka Date: Thu, 20 Jul 2017 12:14:48 +0200 Subject: [PATCH] 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] --- .../websocketx/client/WebSocketClientHandler.java | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) 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 c36283cd8e..fb10576e2e 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 @@ -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.WebSocketClientHandshaker; import io.netty.handler.codec.http.websocketx.WebSocketFrame; +import io.netty.handler.codec.http.websocketx.WebSocketHandshakeException; import io.netty.util.CharsetUtil; public class WebSocketClientHandler extends SimpleChannelInboundHandler { @@ -82,9 +83,14 @@ public class WebSocketClientHandler extends SimpleChannelInboundHandler public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { Channel ch = ctx.channel(); if (!handshaker.isHandshakeComplete()) { - handshaker.finishHandshake(ch, (FullHttpResponse) msg); - System.out.println("WebSocket Client connected!"); - handshakeFuture.setSuccess(); + try { + handshaker.finishHandshake(ch, (FullHttpResponse) msg); + System.out.println("WebSocket Client connected!"); + handshakeFuture.setSuccess(); + } catch (WebSocketHandshakeException e) { + System.out.println("WebSocket Client failed to connect"); + handshakeFuture.setFailure(e); + } return; }