当前位置: 首页 >服务端 > Netty下的WebSocket心跳检测

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 {@Overridepublic 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 **/@Componentpublic class WSServer {public static class SingletionWSServer{static final WSServer instance = new WSServer();}public static WSServer getInstance(){retu 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);}

 

作者:清欢Viki
来源链接:https://blog.csdn.net/weixin_42089175/article/details/99466397

版权声明:
1、JavaClub(https://www.javaclub.cn)以学习交流为目的,由作者投稿、网友推荐和小编整理收藏优秀的IT技术及相关内容,包括但不限于文字、图片、音频、视频、软件、程序等,其均来自互联网,本站不享有版权,版权归原作者所有。

2、本站提供的内容仅用于个人学习、研究或欣赏,以及其他非商业性或非盈利性用途,但同时应遵守著作权法及其他相关法律的规定,不得侵犯相关权利人及本网站的合法权利。
3、本网站内容原作者如不愿意在本网站刊登内容,请及时通知本站(javaclubcn@163.com),我们将第一时间核实后及时予以删除。





本文链接:https://www.javaclub.cn/server/68841.html

标签:Netty
分享给朋友:

“Netty下的WebSocket心跳检测” 的相关文章