@Override public void messageReceived(ChannelInboundHandlerContext<Object> ctx, Object msg) throws Exception { Channel ch = ctx.channel(); if (!handshaker.isHandshakeComplete()) { handshaker.finishHandshake(ch, (HttpResponse) msg); System.out.println("WebSocket Client connected!"); return; } if (msg instanceof HttpResponse) { HttpResponse response = (HttpResponse) msg; throw new Exception( "Unexpected HttpResponse (status=" + response.getStatus() + ", content=" + response.getContent().toString(CharsetUtil.UTF_8) + ")"); } WebSocketFrame frame = (WebSocketFrame) msg; if (frame instanceof TextWebSocketFrame) { TextWebSocketFrame textFrame = (TextWebSocketFrame) frame; System.out.println("WebSocket Client received message: " + textFrame.getText()); } else if (frame instanceof PongWebSocketFrame) { System.out.println("WebSocket Client received pong"); } else if (frame instanceof CloseWebSocketFrame) { System.out.println("WebSocket Client received closing"); ch.close(); } }
private ChannelFuture handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) { // Check for closing frame if (frame instanceof CloseWebSocketFrame) { return Handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain()); } else if (frame instanceof PingWebSocketFrame) { return ctx.channel().write(new PongWebSocketFrame(frame.content().retain())); } else if (frame instanceof TextWebSocketFrame) { TextWebSocketFrame textFrame = (TextWebSocketFrame) frame; String messageText = textFrame.text(); return handleTextMessage(messageText); } else { throw new UnsupportedOperationException( String.format("%s frame types not supported", frame.getClass().getName())); } }
@Override protected void decode( final ChannelHandlerContext channelHandlerContext, final WebSocketFrame webSocketFrame, final List<Object> objects) throws Exception { try { if (webSocketFrame instanceof BinaryWebSocketFrame) { final BinaryWebSocketFrame tf = (BinaryWebSocketFrame) webSocketFrame; objects.add(serializer.deserializeResponse(tf.content())); } else { final TextWebSocketFrame tf = (TextWebSocketFrame) webSocketFrame; final MessageTextSerializer textSerializer = (MessageTextSerializer) serializer; objects.add(textSerializer.deserializeResponse(tf.text())); } } finally { ReferenceCountUtil.release(webSocketFrame); } }
@Override public void onMessage(NClient conn, Object message) { if (message instanceof TextWebSocketFrame) { TextWebSocketFrame f = (TextWebSocketFrame) message; System.out.println(f); System.out.println(f.text()); } else { ByteBuf bb = (ByteBuf) message; bb.readerIndex(conn.getProtocol().headerLen()); byte[] c = new byte[bb.readShort()]; bb.readBytes(c); System.out.println(new String(c)); bb.readerIndex(0); conn.sendFrame(bb); } try { Thread.sleep(500); } catch (Exception e) { } }
@Override protected void channelRead0(ChannelHandlerContext ctx, final TextWebSocketFrame msg) throws Exception { Channel incoming = ctx.channel(); // for (Channel channel : sChannels) { // if (channel != incoming) { // channel.writeAndFlush(new TextWebSocketFrame("[" + incoming.remoteAddress() + // "]" + msg.text())); // } else { // channel.writeAndFlush(new TextWebSocketFrame("[you]" + msg.text())); // } // } final String msgContent = msg.text(); System.out.println("receive cs message"); System.out.println(msgContent); Message message = MessageParser.parseMessage(msgContent); MessageDispatcher.getInst().dispatchMessage(message); }
@Override protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception { group.writeAndFlush(msg.retain()); }
@Override protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception { String text = msg.text(); LOGGER.info("Received message: {}", text); channelProcessor.processEvent(createEvent(text)); }