Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

怎样保持长连接,进行心跳检测呢。 #43

Open
ruo5211314 opened this issue May 31, 2019 · 5 comments
Open

怎样保持长连接,进行心跳检测呢。 #43

ruo5211314 opened this issue May 31, 2019 · 5 comments

Comments

@ruo5211314
Copy link

怎样保持长连接,进行心跳检测呢。

@YeautyYE
Copy link
Owner

客户端定时发送 ping操作帧 即可

@919831081
Copy link

0.8.0版本 手写一个Java客户端 进入ping 没收到服务端消息.

@YeautyYE
Copy link
Owner

0.8.0版本 手写一个Java客户端 进入ping 没收到服务端消息.

你的代码发一下

@919831081
Copy link

image
image
image

用的你们0.8.0版本代码作为服务端,客户端自己写的。
请问SimpleChannelInboundHandler接口如何支持不同类型,如ByteBuf等.
以下为客户端三个类参考Code: CustomHeartbeatHandler、ClientHandler、Client;

package com.crc.crcloud.steam.deploy.netty;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.timeout.IdleStateEvent;

/**

  • @description:

  • @CreateAuthor: biao.sun

  • @Createdate: 2019-10-18 09:40
    */
    public abstract class CustomHeartbeatHandler extends SimpleChannelInboundHandler {
    public static final byte PING_MSG = 1;
    public static final byte PONG_MSG = 2;
    public static final byte CUSTOM_MSG = 3;
    protected String name;
    private int heartbeatCount = 0;

    public CustomHeartbeatHandler(String name) {
    this.name = name;
    }

    @OverRide
    protected void channelRead0(ChannelHandlerContext context, ByteBuf byteBuf) throws Exception {
    if (byteBuf.getByte(4) == PING_MSG) {
    sendPongMsg(context);
    } else if (byteBuf.getByte(4) == PONG_MSG){
    System.out.println(name + " get pong msg from " + context.channel().remoteAddress());
    } else {
    handleData(context, byteBuf);
    }
    }

    protected void sendPingMsg(ChannelHandlerContext context) {
    ByteBuf buf = context.alloc().buffer(5);
    buf.writeInt(5);
    buf.writeByte(PING_MSG);
    context.writeAndFlush(buf);
    heartbeatCount++;
    System.out.println(name + " sent ping msg to " + context.channel().remoteAddress() + ", count: " + heartbeatCount);
    }

    private void sendPongMsg(ChannelHandlerContext context) {
    ByteBuf buf = context.alloc().buffer(5);
    buf.writeInt(5);
    buf.writeByte(PONG_MSG);
    context.channel().writeAndFlush(buf);
    heartbeatCount++;
    System.out.println(name + " sent pong msg to " + context.channel().remoteAddress() + ", count: " + heartbeatCount);
    }

    protected abstract void handleData(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf);

    @OverRide
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {
    // IdleStateHandler 所产生的 IdleStateEvent 的处理逻辑.
    if (evt instanceof IdleStateEvent) {
    IdleStateEvent e = (IdleStateEvent) evt;
    switch (e.state()) {
    case READER_IDLE:
    handleReaderIdle(ctx);
    break;
    case WRITER_IDLE:
    handleWriterIdle(ctx);
    break;
    case ALL_IDLE:
    handleAllIdle(ctx);
    break;
    default:
    break;
    }
    }
    }

    @OverRide
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
    System.err.println("---" + ctx.channel().remoteAddress() + " is active---");
    }

    @OverRide
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
    System.err.println("---" + ctx.channel().remoteAddress() + " is inactive---");
    }

    protected void handleReaderIdle(ChannelHandlerContext ctx) {
    System.err.println("---READER_IDLE---");
    }

    protected void handleWriterIdle(ChannelHandlerContext ctx) {
    System.err.println("---WRITER_IDLE---");
    }

    protected void handleAllIdle(ChannelHandlerContext ctx) {
    System.err.println("---ALL_IDLE---");
    }
    }

package com.crc.crcloud.steam.deploy.netty.client;

import com.crc.crcloud.steam.deploy.netty.CustomHeartbeatHandler;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;

/**

  • @description:

  • @CreateAuthor: biao.sun

  • @Createdate: 2019-10-18 09:47
    */
    public class ClientHandler extends CustomHeartbeatHandler {

    public ClientHandler() {
    super("client");
    }

    @OverRide
    protected void handleData(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) {
    byte[] data = new byte[byteBuf.readableBytes() - 5];
    byteBuf.skipBytes(5);
    byteBuf.readBytes(data);
    String content = new String(data);
    System.out.println(name + " get content: " + content);
    }

    @OverRide
    protected void handleAllIdle(ChannelHandlerContext ctx) {
    super.handleAllIdle(ctx);
    sendPingMsg(ctx);
    }
    }

package com.crc.crcloud.steam.deploy.netty.client;

import com.crc.crcloud.steam.deploy.netty.CustomHeartbeatHandler;
import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.timeout.IdleStateHandler;

import java.util.Random;

/**

  • @description:

  • @CreateAuthor: biao.sun

  • @Createdate: 2019-10-17 14:35
    */
    public class Client {

    public static void main(String[] args) {
    NioEventLoopGroup workGroup = new NioEventLoopGroup(4);
    Random random = new Random(System.currentTimeMillis());
    try {
    Bootstrap bootstrap = new Bootstrap();
    bootstrap
    .group(workGroup)
    .channel(NioSocketChannel.class)
    .handler(new ChannelInitializer() {
    protected void initChannel(SocketChannel socketChannel) throws Exception {
    ChannelPipeline p = socketChannel.pipeline();
    p.addLast(new IdleStateHandler(0, 0, 5));
    p.addLast(new LengthFieldBasedFrameDecoder(1024, 0, 4, -4, 0));
    p.addLast(new ClientHandler());
    p.addLast(new WebSocketServerProtocolHandler("/webSocket/agent/log", null, true, 65536 * 10));
    }
    });

         Channel ch = bootstrap.remoteAddress("10.54.8.35", 18499).connect().sync().channel();
         for (int i = 0; i < 10; i++) {
             String content = "client msg " + i;
             ByteBuf buf = ch.alloc().buffer();
             buf.writeInt(5 + content.getBytes().length);
             buf.writeByte(CustomHeartbeatHandler.CUSTOM_MSG);
             buf.writeBytes(content.getBytes());
             ch.writeAndFlush(buf);
    
             Thread.sleep(random.nextInt(10000));
         }
     } catch (Exception e) {
         throw new RuntimeException(e);
     } finally {
         workGroup.shutdownGracefully();
     }
    

    }

}

@YeautyYE
Copy link
Owner

建议直接使用Netty的PingWebSocketFrame

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants