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 928a9e9eaa
commit 58dc0b2975
1 changed files with 9 additions and 3 deletions

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.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<Object> {
@ -82,9 +83,14 @@ public class WebSocketClientHandler extends SimpleChannelInboundHandler<Object>
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;
}