mitao
2025-02-21 31573d6180d15ef65ed0df9c2732495f40b12663
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.panzhihua.service_property.config;
 
import java.net.URI;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
 
import javax.websocket.ContainerProvider;
import javax.websocket.WebSocketContainer;
 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
 
/**
 * 应用模块名称
 * <p>
 * 一键报警websocket 配置 代码描述
 * <p>
 * Copyright: Copyright (C) 2021 XXX, Inc. All rights reserved.
 * <p>
 * Company: 成都呐喊信息技术有限公司
 * <p>
 *
 * @author manailin
 * @since 2021/9/27 16:31
 */
@Configuration
@Order(1)
public class WebSocketConfig implements ApplicationRunner {
 
    private static final Logger logger = LoggerFactory.getLogger(WebSocketConfig.class);
 
    private static Boolean isOk;
 
    private static WebSocketContainer container = ContainerProvider.getWebSocketContainer();
 
    private WebSocketClient client;
 
    /**
     * 定义定时任务线程
     */
    private ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(1);
 
    @Value("${websocket.uri}")
    private String uri;
 
    @Value("${websocket.ip}")
    private String ip;
 
    /**
     * <run>
     *
     * @param args
     *            args @throws
     */
    @Override
    public void run(ApplicationArguments args) throws Exception {
        logger.info("[WebSocketConfig] web socket init start.");
 
        // websocket客户端初始化
        wsClientInit();
    }
 
    /**
     * <websocket客户端初始化>
     *
     * @throws
     */
    public void wsClientInit() {
        logger.info("[WebSocketConfig] start to wsClientInit");
        try {
            client = new WebSocketClient();
            WebSocketClient.beforeInit();
            container.connectToServer(client, new URI(uri));
 
            isOk = true;
        } catch (Exception e) {
            isOk = false;
            logger.error("error, cause: ", e);
        }
 
        /**
         * 参数:1、任务体 2、首次执行的延时时间 3、任务执行间隔 4、间隔时间单位
         **/
        scheduledExecutorService.scheduleAtFixedRate(new Runnable() {
            @Override
            public void run() {
                // 心跳检测 断线重连
                heartbeatCheck();
            }
        }, 1, 30, TimeUnit.SECONDS);
 
        logger.info("[WebSocketConfig] end to wsClientInit");
    }
 
    /**
     * <心跳检测 断线重连>
     *
     * 
     */
    private void heartbeatCheck() {
        logger.info("[WebSocketConfig] start to heartbeatCheck");
        if (isOk != null && isOk) {
            try {
                client.send("ping " + ip);
            } catch (Exception e) {
                isOk = false;
            }
        } else {
            // 系统连接失败进行重试
            logger.warn("系统连接失败,正在重连...");
            try {
                client.send("ping " + ip);
                logger.warn("系统重连成功!");
                isOk = true;
            } catch (Exception e) {
                try {
                    client = new WebSocketClient();
                    container.connectToServer(client, new URI(uri));
 
                    isOk = true;
                } catch (Exception e1) {
                    isOk = false;
                }
                if (isOk != null && isOk) {
                    logger.warn("系统重连成功!");
                }
            }
        }
    }
}