@Override public void channelRead0(ChannelHandlerContext context, Object message) throws Exception { Channel channel = context.channel(); if (message instanceof FullHttpResponse) { checkState(!handshaker.isHandshakeComplete()); try { handshaker.finishHandshake(channel, (FullHttpResponse) message); delegate.onOpen(); } catch (WebSocketHandshakeException e) { delegate.onError(e); } } else if (message instanceof TextWebSocketFrame) { delegate.onMessage(((TextWebSocketFrame) message).text()); } else { checkState(message instanceof CloseWebSocketFrame); delegate.onClose(); } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { if (cause instanceof WebSocketHandshakeException) { sendBadRequestAndClose(ctx, cause.getMessage()); } else { ctx.close(); } }
@Override @SuppressWarnings("unchecked") public void onInboundNext(ChannelHandlerContext ctx, Object msg) { if (msg instanceof FullHttpResponse) { started = true; channel().pipeline() .remove(HttpObjectAggregator.class); FullHttpResponse response = (FullHttpResponse) msg; setNettyResponse(response); if (checkResponseCode(response)) { try { if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(channel(), response); } } catch (WebSocketHandshakeException wshe) { if (serverError) { onInboundError(wshe); return; } } parentContext().fireContextActive(this); handshakerResult.trySuccess(); } return; } if (msg instanceof PingWebSocketFrame) { channel().writeAndFlush(new PongWebSocketFrame(((PingWebSocketFrame) msg).content())); ctx.read(); return; } if (msg instanceof CloseWebSocketFrame && ((CloseWebSocketFrame)msg).isFinalFragment()) { if (log.isDebugEnabled()) { log.debug("CloseWebSocketFrame detected. Closing Websocket"); } CloseWebSocketFrame close = (CloseWebSocketFrame) msg; sendClose(new CloseWebSocketFrame(true, close.rsv(), close.content())); } else { super.onInboundNext(ctx, msg); } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { if (cause instanceof WebSocketHandshakeException) { log.debug("Web Socket Handshake Exception.", cause); } }