Netty下的WebSocket心跳检测

什么是心跳检测

心跳机制是定时发送一个自定义的结构体(心跳包),让对方知道自己还活着,以确保连接的有效性的机制。

在WebSocket中即判断套接字是否已经与服务器断开,无法使用,此时要清理服务器的该套接字进程以免浪费资源。

心跳包就是客户端定时发送简单的信息给服务器端告诉它还在正常运行。

实例

比如针对客户端每个连接,服务器都会接收并存入一个容器进行统一管理。

客户端的正常结束,服务器会自动清理。

但某些特殊情况下,服务器无法识别。比如打开飞行模式后,关闭客户端,关闭飞行模式,重新打开客户端,

此时容器中并没有清理客户端,而此时又创建了一个客户端连接。

实现

首先创建一个handler,实现心跳。仅检测读写空闲

import io.netty.channel.*;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * @Author Sakura
 * @Date 5/8/2019
 * 用于检测channel心跳的handler
 **/
public class HeartBeatHandler extends ChannelInboundHandlerAdapter {


    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

        //判断evt是否是IdleStateEvent(用于触发用户事件,包含读空闲/写空闲/读写空闲)
        if(evt instanceof IdleState){
            IdleStateEvent idleStateEvent = (IdleStateEvent) evt;

            if(idleStateEvent.state() == IdleState.READER_IDLE){
                System.out.println("进入读空闲...");
            }else if(idleStateEvent.state() == IdleState.WRITER_IDLE){
                System.out.println("进入写空闲...");
            }else if(idleStateEvent.state() == IdleState.ALL_IDLE){
                System.out.println("进入读写空闲...");

                Channel channel = ctx.channel();
                //关闭无用channel,避免浪费资源
                channel.close();
            }
        }
    }
}

后在初始化器中添加handler

package com.imooc.netty;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;
import io.netty.handler.timeout.IdleStateHandler;

/**
 * @Author Sakura
 * @Date 5/8/2019
 **/
public class WSServerInitializer extends ChannelInitializer<SocketChannel> {
    protected void initChannel(SocketChannel socketChannel) throws Exception {

        ChannelPipeline pipeline = socketChannel.pipeline();

        //=============================增加心跳支持============================

        //对客户端,如果在60秒内没有向服务端发送心跳,就主动断开
        //三个参数分别为读/写/读写的空闲,我们只针对读写空闲检测
        pipeline.addLast(new IdleStateHandler(2,4,60));

        pipeline.addLast(new HeartBeatHandler());

    }
}

初始化器是在ServerBootstrap里启动的

package com.imooc.netty;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.stereotype.Component;

/**
 * @Author Sakura
 * @Date 5/8/2019
 **/

@Component
public class WSServer {

    public static class SingletionWSServer{

        static final WSServer instance = new WSServer();
    }

    public static WSServer getInstance(){
        return SingletionWSServer.instance;
    }

    private EventLoopGroup mainGroup;
    private EventLoopGroup subGroup;
    private ServerBootstrap server;
    private ChannelFuture future;

    public WSServer(){
        mainGroup = new NioEventLoopGroup();
        subGroup = new NioEventLoopGroup();
        server = new ServerBootstrap();

        server.group(mainGroup,subGroup)
                .channel(NioServerSocketChannel.class)
                .childHandler(new WSServerInitializer());
    }

    public void start(){
        this.future = server.bind(8088);
        System.err.println("netty websocket server 启动完毕...");
    }

}

接着在前端的WebSocket的onopen事件中定时发送一条数据就好了,对该数据不做处理,让服务器知道有请求发送,客户端还在就好了。

//后端每60秒检测一次,这里只要小于60秒就行了
socket.onopen:function(){
    setInterval("CHAT.keepalive()", 50000);
}

keepalive: function() {
	// 构建对象
	var dataContent = "test alive";
	// 发送心跳
	socket.send(dataContent);
}

 

  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Netty是一个开源的Java网络编程框架,支持多种协议和高性能的网络通信。而WebSocket是一种基于HTTP的协议,能够在浏览器和服务器之间建立全双工通信通道,实现实时的数据传输。在实际开发中,我们常常需要使用Netty来实现WebSocket连接,并通过“心跳”机制来保持连接的稳定性。 “心跳”机制是指在WebSocket连接中,服务器定期向客户端发送特定信息(如一段规定的文本),以检测和保持连接的活跃状态。当客户端接收到心跳消息时,也需要及时回复一个响应消息,以确认连接的正常状态。通过这种方式,可以有效避免长时间未传输数据导致连接失效的情况。 在Netty中,我们可以使用IdleStateHandler类来实现心跳机制。该类可以设置检测的时间间隔和失败次数等参数,并通过自定义ChannelInboundHandler子类的实现来处理空闲超时及心跳消息的发送和接收逻辑。 下面是一个示例代码: ``` public class WebSocketIdleStateHandler extends IdleStateHandler { public WebSocketIdleStateHandler(int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds) { super(readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds); } @Override protected void channelIdle(ChannelHandlerContext ctx, IdleStateEvent evt) throws Exception { // 发送心跳消息 TextWebSocketFrame heartbeat = new TextWebSocketFrame("heartbeat"); ctx.writeAndFlush(heartbeat); } @Override public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception { if (evt instanceof WebSocketFrame) { // 接收心跳消息并回复响应消息 WebSocketFrame frame = (WebSocketFrame) evt; if (frame instanceof TextWebSocketFrame && ((TextWebSocketFrame) frame).text().equals("heartbeat")) { TextWebSocketFrame response = new TextWebSocketFrame("response"); ctx.writeAndFlush(response); } } else { super.userEventTriggered(ctx, evt); } } } ``` 以上代码实现了一个WebSocketIdleStateHandler类,继承自Netty的IdleStateHandler类,用于定时检测连接状态并发送/接收心跳消息。在channelIdle方法中,我们创建一个TextWebSocketFrame对象作为心跳消息,并将其写入到ChannelHandlerContext中。在userEventTriggered方法中,我们判断收到的WebSocketFrame类型是否为TextWebSocketFrame,并检查其内容是否为“heartbeat”。如果是,则认为收到了心跳消息,创建一个响应对象TextWebSocketFrame,将其写入到ChannelHandlerContext中。这样就能保持WebSocket连接的稳定性和实时性了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值