赞
踩
心跳机制是定时发送一个自定义的结构体(心跳包),让对方知道自己还活着,以确保连接的有效性的机制。
在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);
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。