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);
   }
 }
Ejemplo n.º 3
0
  @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);
  }
Ejemplo n.º 4
0
 @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) {
   }
 }
Ejemplo n.º 5
0
 @Override
 protected void channelRead0(ChannelHandlerContext ctx, TextWebSocketFrame msg) throws Exception {
   String text = msg.text();
   LOGGER.info("Received message: {}", text);
   channelProcessor.processEvent(createEvent(text));
 }