Exemplo n.º 1
0
 private Message buildHeatBeat() {
   Message message = new Message();
   Header h = new Header();
   h.setType(MessageType.HEARTBEAT_REQ.value());
   message.setHeader(h);
   return message;
 }
Exemplo n.º 2
0
  @Override
  public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
    Message message = (Message) msg;
    // 握手成功主动发送心跳
    if (message.getHeader() != null
        && message.getHeader().getType() == MessageType.LOGIN_RESP.value()) {

      heartBeat =
          ctx.executor()
              .scheduleAtFixedRate(new HeartBeatTask(ctx), 0, 5000, TimeUnit.MILLISECONDS);

    } else if (message.getHeader() != null
        && message.getHeader().getType() == MessageType.HEARTBEAT_RESP.value()) {

      System.out.println("Client receive server heart beat message:---->" + message);

    } else {
      ctx.fireChannelRead(message);
    }
  }