vince
2023-07-01 7d84b6cbf8268a6993f661e22a842ffa12c13732
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.ld.igds.protocol.bhzn.verb.server;
 
 
import com.ld.igds.protocol.bhzn.verb.server.BhznVerbServerUtils;
import com.ld.io.api.HeartbeatProvider;
import com.ld.io.api.IoSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
/**
 * 提供心跳信息
 *
 * @author Andy
 */
@Slf4j
@Service
public class BhznVerbHeartbeatImpl implements HeartbeatProvider {
 
    @Override
    public byte[] provide(IoSession session) {
 
 
        //直接根据主机SESSIOn判断
        //IoSession session1 = BhznVerbServerUtils.getSession();
 
        Long lastBeatTime = BhznVerbServerUtils.getHearBeat(session);
 
        if (null == lastBeatTime) {
            return null;
        }
 
        //如果最后一次终端心跳时间距离当前时间大于 终端心跳时间的3次,则判断为离线
        long deTime = System.currentTimeMillis() - lastBeatTime;
 
        if (deTime > (BhznVerbServerUtils.HEART_BEAT_TIME * 3 * 1000)) {
            log.info("-----------超过{}秒未获取到心跳信息,判定终端离线,踢出连接------------{}-{}", deTime, session.getAddress(), session.getPort());
 
            session.destroy();
        }
 
        return null;
    }
}