コード例 #1
0
  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()));
    }
  }
コード例 #2
0
  private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) {

    System.out.println("pre1:" + frame);

    // Check for closing frame
    if (frame instanceof CloseWebSocketFrame) {
      handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain());
      return;
    }
    if (frame instanceof PingWebSocketFrame) {
      ctx.channel().write(new PongWebSocketFrame(frame.content().retain()));
      return;
    }
    if (!(frame instanceof TextWebSocketFrame)) {
      throw new UnsupportedOperationException(
          String.format("%s frame types not supported", frame.getClass().getName()));
    }

    WebsocketMessageHandlerI websocketMessageHandler = new WebSocketMessageHander(ctx, channels);
    // Send the uppercase string back.

    String requestRaw = ((TextWebSocketFrame) frame).text();

    websocketMessageHandler.handleMessage(requestRaw);
  }
コード例 #3
0
  @Override
  public void channelRead0(final ChannelHandlerContext c, Object msg) throws Exception {
    Channel ch = ctx.channel();
    if (response == null) {
      if (msg instanceof FullHttpResponse) {
        response = (FullHttpResponse) msg;
      } else {
        response = new WSResponse((DefaultHttpResponse) msg, ctx.alloc().buffer());
      }
      if (completeHandshake(ctx)) {
        return;
      }
    }
    if (msg instanceof LastHttpContent) {
      return;
    }

    if (!(msg instanceof WebSocketFrame)) {
      throw new Exception(
          "Unexpected FullHttpResponse (getStatus="
              + response.getStatus()
              + ", "
              + "content="
              + response.content().toString(CharsetUtil.UTF_8)
              + ')');
    }

    final WebSocketFrame frame = (WebSocketFrame) msg;
    if (frame instanceof TextWebSocketFrame) {
      for (WebSocketEventListener l : listensers) {
        l.onMessage(c, new WebSocketMessage(((TextWebSocketFrame) frame).text()));
      }
    } else if (frame instanceof PingWebSocketFrame) {
      if (autoPong) {
        ctx.writeAndFlush(new PongWebSocketFrame(frame.content().copy()));
      }
      for (WebSocketEventListener l : listensers) {
        l.onPing(c, (PingWebSocketFrame) frame.copy());
      }
    } else if (frame instanceof PongWebSocketFrame) {
      Logger.getLogger(getClass())
          .warn(
              String.format(
                  "WebSocketClient received a PongWebSocketFrame, that shouldn't happen! Data : %s",
                  frame.content().toString(CharsetUtil.UTF_8)));
    } else if (frame instanceof CloseWebSocketFrame) {
      ch.close();
      for (WebSocketEventListener l : listensers) {
        l.onClose(c, (CloseWebSocketFrame) frame.copy());
      }
    }
  }
コード例 #4
0
  @Override
  public void handle(Channel channel, NettyResponseFuture<?> future, Object e) throws Exception {

    if (e instanceof HttpResponse) {
      HttpResponse response = (HttpResponse) e;
      Channels.setAttribute(channel, new UpgradeCallback(future, channel, response));

    } else if (e instanceof WebSocketFrame) {

      final WebSocketFrame frame = (WebSocketFrame) e;
      WebSocketUpgradeHandler handler =
          WebSocketUpgradeHandler.class.cast(future.getAsyncHandler());
      NettyWebSocket webSocket = NettyWebSocket.class.cast(handler.onCompleted());
      invokeOnSucces(channel, handler);

      if (webSocket != null) {
        if (frame instanceof CloseWebSocketFrame) {
          Channels.setDiscard(channel);
          CloseWebSocketFrame closeFrame = CloseWebSocketFrame.class.cast(frame);
          webSocket.onClose(closeFrame.statusCode(), closeFrame.reasonText());
        } else {
          ByteBuf buf = frame.content();
          if (buf != null && buf.readableBytes() > 0) {
            try {
              NettyResponseBodyPart part =
                  nettyConfig
                      .getBodyPartFactory()
                      .newResponseBodyPart(buf, frame.isFinalFragment());
              handler.onBodyPartReceived(part);

              if (frame instanceof BinaryWebSocketFrame) {
                webSocket.onBinaryFragment(part);
              } else if (frame instanceof TextWebSocketFrame) {
                webSocket.onTextFragment(part);
              } else if (frame instanceof PingWebSocketFrame) {
                webSocket.onPing(part);
              } else if (frame instanceof PongWebSocketFrame) {
                webSocket.onPong(part);
              }
            } finally {
              buf.release();
            }
          }
        }
      } else {
        logger.debug("UpgradeHandler returned a null NettyWebSocket ");
      }
    } else {
      logger.error("Invalid message {}", e);
    }
  }
コード例 #5
0
 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()));
   }
 }
コード例 #6
0
  private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) {
    // Check for closing frame
    if (frame instanceof CloseWebSocketFrame) {
      handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain());
      return;
    }

    if (frame instanceof PingWebSocketFrame) {
      ctx.channel().writeAndFlush(new PongWebSocketFrame(frame.content().retain()));
      return;
    }

    if (frame instanceof ContinuationWebSocketFrame) return;

    if (frame instanceof TextWebSocketFrame)
      webSocketActor.onMessage(((TextWebSocketFrame) frame).text());
    else webSocketActor.onMessage(frame.content().nioBuffer());
  }
コード例 #7
0
  private void handlerWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) {

    // �ж��Ƿ�ر���·��ָ��
    if (frame instanceof CloseWebSocketFrame) {
      handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain());
    }

    // �ж��Ƿ�ping��Ϣ
    if (frame instanceof PingWebSocketFrame) {
      ctx.channel().write(new PongWebSocketFrame(frame.content().retain()));
      return;
    }

    // �����̽�֧���ı���Ϣ����֧�ֶ�������Ϣ
    if (!(frame instanceof TextWebSocketFrame)) {

      System.out.println("�����̽�֧���ı���Ϣ����֧�ֶ�������Ϣ");

      throw new UnsupportedOperationException(
          String.format("%s frame types not supported", frame.getClass().getName()));
    }

    // ����Ӧ����Ϣ
    String request = ((TextWebSocketFrame) frame).text();

    System.out.println("������յ���" + request);

    if (logger.isLoggable(Level.FINE)) {
      logger.fine(String.format("%s received %s", ctx.channel(), request));
    }

    TextWebSocketFrame tws =
        new TextWebSocketFrame(new Date().toString() + ctx.channel().id() + "��" + request);

    // Ⱥ��
    Global.group.writeAndFlush(tws);

    // ���ء�˭���ķ���˭��
    // ctx.channel().writeAndFlush(tws);

  }
コード例 #8
0
  private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) {

    // Check for closing frame
    if (frame instanceof CloseWebSocketFrame) {
      handshaker.close(ctx.channel(), (CloseWebSocketFrame) frame.retain());
      return;
    }
    if (frame instanceof PingWebSocketFrame) {
      ctx.channel().write(new PongWebSocketFrame(frame.content().retain()));
      return;
    }
    if (!(frame instanceof TextWebSocketFrame)) {
      throw new UnsupportedOperationException(
          String.format("%s frame types not supported", frame.getClass().getName()));
    }

    // Send the uppercase string back.
    String request = ((TextWebSocketFrame) frame).text();
    System.err.printf("%s received %s%n", ctx.channel(), request);
    ctx.channel().write(new TextWebSocketFrame(request.toUpperCase()));
  }
コード例 #9
0
  private void handleWebSocketFrame(ChannelHandlerContext ctx, WebSocketFrame frame) {
    logger.debug(
        String.format(
            "Channel %s received %s", ctx.getChannel().getId(), frame.getClass().getSimpleName()));

    if (frame instanceof CloseWebSocketFrame) {
      this.handshaker.performClosingHandshake(ctx, (CloseWebSocketFrame) frame);
    } else if (frame instanceof PingWebSocketFrame) {
      ctx.getChannel()
          .write(
              new PongWebSocketFrame(
                  frame.isFinalFragment(), frame.getRsv(), frame.getBinaryData()));
    } else if (frame instanceof TextWebSocketFrame) {
      // String text = ((TextWebSocketFrame) frame).getText();
      ctx.getChannel()
          .write(
              new TextWebSocketFrame(
                  frame.isFinalFragment(), frame.getRsv(), frame.getBinaryData()));
    } else if (frame instanceof BinaryWebSocketFrame) {
      ctx.getChannel()
          .write(
              new BinaryWebSocketFrame(
                  frame.isFinalFragment(), frame.getRsv(), frame.getBinaryData()));
    } else if (frame instanceof ContinuationWebSocketFrame) {
      ctx.getChannel()
          .write(
              new ContinuationWebSocketFrame(
                  frame.isFinalFragment(), frame.getRsv(), frame.getBinaryData()));
    } else if (frame instanceof PongWebSocketFrame) {
      // Ignore
    } else {
      throw new UnsupportedOperationException(
          String.format("%s frame types not supported", frame.getClass().getName()));
    }
  }