private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { if (logger.isLoggable(Level.FINE)) { logger.fine(String.format( "Channel %s received %s", ctx.channel().hashCode(), StringUtil.simpleClassName(frame))); } if (frame instanceof CloseWebSocketFrame) { handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame); } else if (frame instanceof PingWebSocketFrame) { ctx.write(new PongWebSocketFrame(frame.isFinalFragment(), frame.rsv(), frame.content()), ctx.voidPromise()); } else if (frame instanceof TextWebSocketFrame) { ctx.write(frame, ctx.voidPromise()); } else if (frame instanceof BinaryWebSocketFrame) { ctx.write(frame, ctx.voidPromise()); } else if (frame instanceof ContinuationWebSocketFrame) { ctx.write(frame, ctx.voidPromise()); } else if (frame instanceof PongWebSocketFrame) { frame.release(); // Ignore } else { throw new UnsupportedOperationException(String.format("%s frame types not supported", frame.getClass() .getName())); } }
@Override public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception { LOG.trace("NettyServerHandler: Channel write: {}", msg); if (isWebSocketServer() && msg instanceof ByteBuf) { if(isFragmentWrites()) { ByteBuf orig = (ByteBuf) msg; int origIndex = orig.readerIndex(); int split = orig.readableBytes()/2; ByteBuf part1 = orig.copy(origIndex, split); LOG.trace("NettyServerHandler: Part1: {}", part1); orig.readerIndex(origIndex + split); LOG.trace("NettyServerHandler: Part2: {}", orig); BinaryWebSocketFrame frame1 = new BinaryWebSocketFrame(false, 0, part1); ctx.writeAndFlush(frame1); ContinuationWebSocketFrame frame2 = new ContinuationWebSocketFrame(true, 0, orig); ctx.write(frame2, promise); } else { BinaryWebSocketFrame frame = new BinaryWebSocketFrame((ByteBuf) msg); ctx.write(frame, promise); } } else { ctx.write(msg, promise); } }
private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { if (logger.isLoggable(Level.FINE)) { logger.fine(String.format( "Channel %s received %s", ctx.channel().hashCode(), StringUtil.simpleClassName(frame))); } if (frame instanceof CloseWebSocketFrame) { handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame); } else if (frame instanceof PingWebSocketFrame) { ctx.write(new PongWebSocketFrame(frame.isFinalFragment(), frame.rsv(), frame.content())); } else if (frame instanceof TextWebSocketFrame) { ctx.write(frame); } else if (frame instanceof BinaryWebSocketFrame) { ctx.write(frame); } else if (frame instanceof ContinuationWebSocketFrame) { ctx.write(frame); } else if (frame instanceof PongWebSocketFrame) { frame.release(); // Ignore } else { throw new UnsupportedOperationException(String.format("%s frame types not supported", frame.getClass() .getName())); } }
protected void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { logger.debug("Received incoming frame [{}]", frame.getClass().getName()); // Check for closing frame if (frame instanceof CloseWebSocketFrame) { if (frameBuffer != null) { handleMessageCompleted(ctx, frameBuffer.toString()); } handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); return; } if (frame instanceof PingWebSocketFrame) { ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content().retain())); return; } if (frame instanceof PongWebSocketFrame) { logger.info("Pong frame received"); return; } if (frame instanceof TextWebSocketFrame) { frameBuffer = new StringBuilder(); frameBuffer.append(((TextWebSocketFrame)frame).text()); } else if (frame instanceof ContinuationWebSocketFrame) { if (frameBuffer != null) { frameBuffer.append(((ContinuationWebSocketFrame)frame).text()); } else { logger.warn("Continuation frame received without initial frame."); } } else { throw new UnsupportedOperationException(String.format("%s frame types not supported", frame.getClass().getName())); } // Check if Text or Continuation Frame is final fragment and handle if needed. if (frame.isFinalFragment()) { handleMessageCompleted(ctx, frameBuffer.toString()); frameBuffer = null; } }
protected void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { logger.debug("Received incoming frame [{}]", frame.getClass().getName()); // Check for closing frame if ( frame instanceof CloseWebSocketFrame) { if ( frameBuffer != null) { handleMessageCompleted( ctx, frameBuffer.toString()); } handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); return; } if (frame instanceof PingWebSocketFrame) { ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content().retain())); return; } if (frame instanceof PongWebSocketFrame) { logger.info("Pong frame received"); return; } if (frame instanceof TextWebSocketFrame) { frameBuffer = new StringBuilder(); frameBuffer.append(((TextWebSocketFrame)frame).text()); } else if (frame instanceof ContinuationWebSocketFrame) { if (frameBuffer != null) { frameBuffer.append(((ContinuationWebSocketFrame)frame).text()); } else { logger.warn("Continuation frame received without initial frame."); } } else { throw new UnsupportedOperationException(String.format("%s frame types not supported", frame.getClass().getName())); } // Check if Text or Continuation Frame is final fragment and handle if needed. if (frame.isFinalFragment()) { handleMessageCompleted(ctx, frameBuffer.toString()); frameBuffer = null; } }
/** * Handle web socket frame. * * @param ctx the ctx * @param frame the frame */ private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { try{ // Check for closing frame if (frame instanceof CloseWebSocketFrame) { dominoServer.onClose(this.newWrapper(ctx)); handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); return; } if (frame instanceof PingWebSocketFrame) { ctx.channel().write(new PongWebSocketFrame(frame.content().retain())); return; } if(frame instanceof PongWebSocketFrame){ return;//do nothing. } if(frame instanceof TextWebSocketFrame){ String message = ((TextWebSocketFrame) frame).text(); textBuffer.append(message); }else if(frame instanceof ContinuationWebSocketFrame){ textBuffer.append(((ContinuationWebSocketFrame) frame).text()); } if(frame.isFinalFragment()){ dominoServer.onMessage(this.newWrapper(ctx), textBuffer.toString()); textBuffer = new StringBuilder(); } }catch(Exception e){ e.printStackTrace(); } }
@Override protected void channelRead0(ChannelHandlerContext ctx, Object message) throws Exception { LOG.trace("New data read: incoming: {}", message); Channel ch = ctx.channel(); if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ch, (FullHttpResponse) message); LOG.trace("WebSocket Client connected! {}", ctx.channel()); // Now trigger super processing as we are really connected. NettyWSTransport.super.handleConnected(ch); return; } // We shouldn't get this since we handle the handshake previously. if (message instanceof FullHttpResponse) { FullHttpResponse response = (FullHttpResponse) message; throw new IllegalStateException( "Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" + response.content().toString(StandardCharsets.UTF_8) + ')'); } WebSocketFrame frame = (WebSocketFrame) message; if (frame instanceof TextWebSocketFrame) { TextWebSocketFrame textFrame = (TextWebSocketFrame) frame; LOG.warn("WebSocket Client received message: " + textFrame.text()); ctx.fireExceptionCaught(new IOException("Received invalid frame over WebSocket.")); } else if (frame instanceof BinaryWebSocketFrame) { BinaryWebSocketFrame binaryFrame = (BinaryWebSocketFrame) frame; LOG.trace("WebSocket Client received data: {} bytes", binaryFrame.content().readableBytes()); listener.onData(binaryFrame.content()); } else if (frame instanceof ContinuationWebSocketFrame) { ContinuationWebSocketFrame continuationFrame = (ContinuationWebSocketFrame) frame; LOG.trace("WebSocket Client received data continuation: {} bytes", continuationFrame.content().readableBytes()); listener.onData(continuationFrame.content()); } else if (frame instanceof PingWebSocketFrame) { LOG.trace("WebSocket Client received ping, response with pong"); ch.write(new PongWebSocketFrame(frame.content())); } else if (frame instanceof CloseWebSocketFrame) { LOG.trace("WebSocket Client received closing"); ch.close(); } }
@Override public void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { Channel ch = ctx.channel(); if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ch, (FullHttpResponse) msg); log.debug("{} WebSocket Client connected!", label); 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; if (textFrame.isFinalFragment()) { receivedTextMessage(textFrame.text()); } else { partialText.append(textFrame.text()); } } else if (frame instanceof ContinuationWebSocketFrame) { ContinuationWebSocketFrame continuationFrame = (ContinuationWebSocketFrame) frame; partialText.append(continuationFrame.text()); if (continuationFrame.isFinalFragment()) { receivedTextMessage(partialText.toString()); partialText.setLength(0); } } else if (frame instanceof CloseWebSocketFrame) { CloseWebSocketFrame closeFrame = (CloseWebSocketFrame) frame; log.info("{} Received close frame from server. Will close client! Reason: {}", label, closeFrame.reasonText()); } else { log.warn("{} Received frame of type {}. Will be ignored", label, frame.getClass().getSimpleName()); } }
@Override protected void channelRead0(ChannelHandlerContext ctx, Object message) throws Exception { LOG.trace("New data read: incoming: {}", message); Channel ch = ctx.channel(); if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ch, (FullHttpResponse) message); LOG.trace("WebSocket Client connected! {}", ctx.channel()); // Now trigger super processing as we are really connected. NettyWsTransport.super.handleConnected(ch); return; } // We shouldn't get this since we handle the handshake previously. if (message instanceof FullHttpResponse) { FullHttpResponse response = (FullHttpResponse) message; throw new IllegalStateException( "Unexpected FullHttpResponse (getStatus=" + response.status() + ", content=" + response.content().toString(StandardCharsets.UTF_8) + ')'); } WebSocketFrame frame = (WebSocketFrame) message; if (frame instanceof TextWebSocketFrame) { TextWebSocketFrame textFrame = (TextWebSocketFrame) frame; LOG.warn("WebSocket Client received message: " + textFrame.text()); ctx.fireExceptionCaught(new IOException("Received invalid frame over WebSocket.")); } else if (frame instanceof BinaryWebSocketFrame) { BinaryWebSocketFrame binaryFrame = (BinaryWebSocketFrame) frame; LOG.trace("WebSocket Client received data: {} bytes", binaryFrame.content().readableBytes()); listener.onData(binaryFrame.content()); } else if (frame instanceof ContinuationWebSocketFrame) { ContinuationWebSocketFrame continuationFrame = (ContinuationWebSocketFrame) frame; LOG.trace("WebSocket Client received data continuation: {} bytes", continuationFrame.content().readableBytes()); listener.onData(continuationFrame.content()); } else if (frame instanceof PingWebSocketFrame) { LOG.trace("WebSocket Client received ping, response with pong"); ch.write(new PongWebSocketFrame(frame.content())); } else if (frame instanceof CloseWebSocketFrame) { LOG.trace("WebSocket Client received closing"); ch.close(); } }
@Override protected void messageReceived(ChannelHandlerContext ctx, ContinuationWebSocketFrame msg) throws Exception { }