@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (!(msg instanceof FullHttpResponse)) { ctx.fireChannelRead(msg); return; } FullHttpResponse response = (FullHttpResponse) msg; try { if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ctx.channel(), response); ctx.fireUserEventTriggered( WebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_COMPLETE); ctx.pipeline().remove(this); return; } throw new IllegalStateException("WebSocketClientHandshaker should have been non finished yet"); } finally { response.release(); } } }
@Override public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { Channel ch = ctx.channel(); if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ch, (FullHttpResponse) msg); LOGGER.debug("WebSocket Client connected!"); handshakeFuture.setSuccess(); return; } if (msg instanceof FullHttpResponse) { FullHttpResponse response = (FullHttpResponse) msg; throw new IllegalStateException("Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')'); } WebSocketFrame frame = (WebSocketFrame) msg; if (frame instanceof TextWebSocketFrame) { TextWebSocketFrame textFrame = (TextWebSocketFrame) frame; LOGGER.debug("WebSocket Client received text message: " + textFrame.text()); String textReceived = textFrame.text(); callback.call(textReceived); } else if (frame instanceof CloseWebSocketFrame) { isConnected = false; LOGGER.debug("WebSocket Client received closing"); ch.close(); } }
@Override public void channelRead0(ChannelHandlerContext ctx, Object msg) { Channel ch = ctx.channel(); if (msg instanceof FullHttpResponse) { FullHttpResponse httpResponse = (FullHttpResponse) msg; final SettableFuture<String> registrationFuture = ch.attr(REGISTRATION_FUTURE).get(); if (httpResponse.headers().contains(UPGRADE, WEBSOCKET, true) && !handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ch, httpResponse); final String clientRegistrationId = httpResponse.headers().get("X-CLIENT-REGISTRATION-ID"); registrationFuture.set(clientRegistrationId); mockServerLogger.trace("web socket client " + clientRegistrationId + " connected!"); } else if (httpResponse.status().equals(HttpResponseStatus.NOT_IMPLEMENTED)) { String message = readRequestBody(httpResponse); registrationFuture.setException(new WebSocketException(message)); mockServerLogger.warn(message); } else { registrationFuture.setException(new WebSocketException("Unsupported web socket message " + new FullHttpResponseToMockServerResponse().mapMockServerResponseToFullHttpResponse(httpResponse))); } } else if (msg instanceof WebSocketFrame) { WebSocketFrame frame = (WebSocketFrame) msg; if (frame instanceof TextWebSocketFrame) { webSocketClient.receivedTextWebSocketFrame((TextWebSocketFrame) frame); } else if (frame instanceof PingWebSocketFrame) { ctx.write(new PongWebSocketFrame(frame.content().retain())); } else if (frame instanceof CloseWebSocketFrame) { mockServerLogger.trace("web socket client received request to close"); ch.close(); } } }
@Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) { final Channel ch = ctx.channel(); if (!handshaker.isHandshakeComplete()) {
@Override protected void channelRead0(final ChannelHandlerContext ctx, final Object msg) throws Exception { final Channel ch = ctx.channel(); if (!handshaker.isHandshakeComplete()) { // web socket client connected handshaker.finishHandshake(ch, (FullHttpResponse) msg); handshakeFuture.setSuccess(); return; } if (msg instanceof FullHttpResponse) { final FullHttpResponse response = (FullHttpResponse) msg; throw new Exception("Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')'); } // a close frame doesn't mean much here. errors raised from closed channels will mark the host as dead final WebSocketFrame frame = (WebSocketFrame) msg; if (frame instanceof TextWebSocketFrame) { ctx.fireChannelRead(frame.retain(2)); } else if (frame instanceof PingWebSocketFrame) { ctx.writeAndFlush(new PongWebSocketFrame()); }else if (frame instanceof PongWebSocketFrame) { logger.debug("Received response from keep-alive request"); } else if (frame instanceof BinaryWebSocketFrame) { ctx.fireChannelRead(frame.retain(2)); } else if (frame instanceof CloseWebSocketFrame) ch.close(); }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Channel ch = ctx.channel(); if (!handshaker.isHandshakeComplete()) { finishHandshake(ctx, (FullHttpResponse) msg, ch); } else { ctx.fireChannelRead(msg); } }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { Channel ch = ctx.channel(); if (!handshaker.isHandshakeComplete()) { finishHandshake(ctx, (FullHttpResponse) msg, ch); } else { ctx.fireChannelRead(msg); } }
@Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception{ if(handshaker.isHandshakeComplete()) super.exceptionCaught(ctx, cause); else{ connectListener.onError(cause); ctx.close(); } }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (!(msg instanceof FullHttpResponse)) { ctx.fireChannelRead(msg); return; } FullHttpResponse response = (FullHttpResponse) msg; try { if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ctx.channel(), response); ctx.fireUserEventTriggered( WebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_COMPLETE); ctx.pipeline().remove(this); return; } throw new IllegalStateException("WebSocketClientHandshaker should have been non finished yet"); } finally { response.release(); } } }
@Override public void channelRead0(ChannelHandlerContext ctx, Object msg) { System.out.println("client channelRead0 "+ctx); Channel ch = ctx.channel(); if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ch, (FullHttpResponse) msg); System.out.println("WebSocket Client connected!"); handshakeFuture.setSuccess(); } if(msg instanceof WebSocketFrame){ WebSocketFrame frame = (WebSocketFrame)msg; if(frame instanceof BinaryWebSocketFrame){ handleWebSocketFrame(ctx, frame); } } }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (!(msg instanceof FullHttpResponse)) { ctx.fireChannelRead(msg); return; } FullHttpResponse response = (FullHttpResponse) msg; try { if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ctx.channel(), response); ctx.fireUserEventTriggered( WebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_COMPLETE); ctx.pipeline().remove(this); return; } throw new IllegalStateException("WebSocketClientHandshaker should have been non finished yet"); } finally { response.release(); } } }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (!(msg instanceof FullHttpResponse)) { ctx.fireChannelRead(msg); return; } FullHttpResponse response = (FullHttpResponse) msg; try { if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ctx.channel(), response); ctx.fireUserEventTriggered( WebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_COMPLETE); ctx.pipeline().remove(this); return; } throw new IllegalStateException("WebSocketClientHandshaker should have been non finished yet"); } finally { response.release(); } } }
@Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { if (!(msg instanceof FullHttpResponse)) { ctx.fireChannelRead(msg); return; } FullHttpResponse response = (FullHttpResponse) msg; try { if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ctx.channel(), response); ctx.fireUserEventTriggered( WebSocketClientProtocolHandler.ClientHandshakeStateEvent.HANDSHAKE_COMPLETE); ctx.pipeline().remove(this); return; } throw new IllegalStateException("WebSocketClientHandshaker should have been non finished yet"); } finally { response.release(); } } }
@Override public void channelRead0(final ChannelHandlerContext ctx, final Object msg) { final Channel ch = ctx.channel(); if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ch, (FullHttpResponse) msg); handshakeFuture.setSuccess(); return; } if (msg instanceof FullHttpResponse) { FullHttpResponse response = (FullHttpResponse) msg; throw new IllegalStateException( "Unexpected FullHttpResponse (getStatus=" + response.getStatus() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')'); } final WebSocketFrame frame = (WebSocketFrame) msg; if (frame instanceof TextWebSocketFrame) { ctx.fireChannelRead(frame.retain()); } else if (frame instanceof CloseWebSocketFrame) { ch.close(); } }
@Override public void channelRead0(ChannelHandlerContext ctx, Object msg) { System.out.println("client channelRead0 "+ctx); Channel ch = ctx.channel(); if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ch, (FullHttpResponse) msg); System.out.println("WebSocket Client connected!"); handshakeFuture.setSuccess(); } if(msg instanceof WebSocketFrame){ WebSocketFrame frame = (WebSocketFrame)msg; if(frame instanceof BinaryWebSocketFrame){ handleWebSocketFrame(frame); } return; } sendRealTimeMessageTest(ctx); }
@Override protected void channelRead0(final ChannelHandlerContext ctx, final Object msg) throws Exception { final Channel ch = ctx.channel(); if (!handshaker.isHandshakeComplete()) { // web socket client connected handshaker.finishHandshake(ch, (FullHttpResponse) msg); handshakeFuture.setSuccess(); return; } if (msg instanceof FullHttpResponse) { final FullHttpResponse response = (FullHttpResponse) msg; throw new Exception("Unexpected FullHttpResponse (getStatus=" + response.getStatus() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')'); } // a close frame doesn't mean much here. errors raised from closed channels will mark the host as dead final WebSocketFrame frame = (WebSocketFrame) msg; if (frame instanceof TextWebSocketFrame) { ctx.fireChannelRead(frame.retain(2)); } else if (frame instanceof PongWebSocketFrame) { } else if (frame instanceof BinaryWebSocketFrame) { ctx.fireChannelRead(frame.retain(2)); } else if (frame instanceof CloseWebSocketFrame) ch.close(); }
protected boolean completeHandshake(ChannelHandlerContext ctx) { if (!handshaker.isHandshakeComplete()) { if (response != null && response.getStatus().code() > 299) { for (WebSocketEventListener l : listensers) { l.onError(ctx, null, response); } return true; } handshaker.finishHandshake(ctx.channel(), response); handshakeFuture.setSuccess(); for (WebSocketEventListener l : listensers) { l.onConnect(ctx); } return true; } return false; }
protected boolean completeHandshake(ChannelHandlerContext ctx) { if (!handshaker.isHandshakeComplete()) { if (response != null && response.getStatus().code() > 299) { for (WebSocketEventListener l : listensers) { l.onError(ctx, null, response); } return true; } handshaker.finishHandshake(ctx.channel(), response); handshakeFuture.setSuccess(); for (WebSocketEventListener l : listensers) { l.onConnect(ctx); } return true; } return false; }
@Override public void channelRead0(ChannelHandlerContext context, Object message) { 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 protected void channelRead0(ChannelHandlerContext context, Object message) throws Exception { final Channel channel = context.channel(); if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(channel, (FullHttpResponse) message); channel.pipeline().addBefore(HANDLER_NAME, "websocket-frame-aggregator", new WebSocketFrameAggregator(64 * 1024)); subscriber.onStart(); return; } if (message instanceof FullHttpResponse) { final FullHttpResponse response = (FullHttpResponse) message; throw new IllegalStateException( "Unexpected FullHttpResponse (getStatus=" + response.getStatus() + ", content=" + response.content().toString(CharsetUtil.UTF_8) + ')'); } final WebSocketFrame frame = (WebSocketFrame) message; if (frame instanceof PingWebSocketFrame) { context.writeAndFlush(new PongWebSocketFrame(((PingWebSocketFrame)frame).retain().content())); } else if (frame instanceof BinaryWebSocketFrame) { final ByteBufInputStream input = new ByteBufInputStream(((BinaryWebSocketFrame)message).content()); final Envelope envelope = Envelope.ADAPTER.decode(input); subscriber.onNext(envelope); } }