vince
2024-12-13 d3e48589ea2719cdde41b87fc1ad80f4ee509bac
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
44
package com.fzzy.protocol.fzzy.server;
 
import com.fzzy.api.Constant;
import com.fzzy.api.data.ApiCommonDevice;
import com.fzzy.api.utils.SpringUtil;
import com.fzzy.gateway.service.GatewayDeviceService;
import com.ld.io.api.IoSession;
import com.ld.io.api.IoSessionListener;
import lombok.extern.slf4j.Slf4j;
 
import javax.annotation.Resource;
 
@Slf4j
public class SessionListener implements IoSessionListener {
 
    private GatewayDeviceService gatewayDeviceService;
 
    @Override
    public void onCreate(IoSession session) {
 
        log.info("++++FZZY-控制个协议有新设备连接++++-IP={},PORT={}", session.getAddress(), session.getPort());
 
        // 添加自定义业务ID
        session.setBusinessKey(ServerUtils.getServerKey(session.getAddress(), session.getPort()));
 
    }
 
    @Override
    public void onDestroy(IoSession session) {
        log.info("----FZZY-控制个协议有设备离线-----IP={},PORT={}", session.getAddress(), session.getPort());
 
        //设置分机掉线
        ApiCommonDevice commonDevice = Constant.updateCacheOffline(session.getAddress(), session.getPort());
 
        if (null == commonDevice) return;
 
        if (null == gatewayDeviceService) {
            gatewayDeviceService = SpringUtil.getBean(GatewayDeviceService.class);
 
            gatewayDeviceService.OfflineByCommonDevice(commonDevice);
        }
 
    }
}