com.zhuang.netty.handler.heartbeat.HeartbeatServerHandler Maven / Gradle / Ivy
package com.zhuang.netty.handler.heartbeat;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class HeartbeatServerHandler extends ChannelInboundHandlerAdapter {
private static final ByteBuf HEARTBEAT_SEQUENCE = Unpooled.unreleasableBuffer(Unpooled.copiedBuffer("HEARTBEAT", CharsetUtil.UTF_8));
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
if (msg instanceof ByteBuf && msg.equals(HEARTBEAT_SEQUENCE)) {
// 收到客户端的心跳响应消息
log.info("Server receive heartbeat");
ctx.writeAndFlush(HEARTBEAT_SEQUENCE);
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy