|
@@ -0,0 +1,46 @@
|
|
1
|
+package com.huiju.welcome.hardware.libs;
|
|
2
|
+
|
|
3
|
+import com.huiju.welcome.hardware.websocket.Client;
|
|
4
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
5
|
+import org.springframework.stereotype.Component;
|
|
6
|
+
|
|
7
|
+import java.net.URI;
|
|
8
|
+import java.util.Timer;
|
|
9
|
+import java.util.TimerTask;
|
|
10
|
+
|
|
11
|
+@Component
|
|
12
|
+public class HeartMonitor implements Runnable {
|
|
13
|
+ Client wsClient;
|
|
14
|
+
|
|
15
|
+ @Autowired
|
|
16
|
+ public HeartMonitor() {
|
|
17
|
+ initWSClient();
|
|
18
|
+ }
|
|
19
|
+
|
|
20
|
+ private void initWSClient() {
|
|
21
|
+ String url = Configer.pick("monitorhub.websocket");
|
|
22
|
+ if (null == url || "".equals(url.trim())) {
|
|
23
|
+ return;
|
|
24
|
+ }
|
|
25
|
+
|
|
26
|
+ url += Configer.pick("monitorhub.appid");
|
|
27
|
+
|
|
28
|
+ wsClient = new Client(URI.create(url));
|
|
29
|
+ wsClient.connect();
|
|
30
|
+ }
|
|
31
|
+
|
|
32
|
+ @Override
|
|
33
|
+ public void run() {
|
|
34
|
+ if (null == wsClient) {
|
|
35
|
+ return;
|
|
36
|
+ }
|
|
37
|
+
|
|
38
|
+ // 定时器
|
|
39
|
+ Timer timer = new Timer();
|
|
40
|
+ timer.schedule(new TimerTask() {
|
|
41
|
+ public void run() {
|
|
42
|
+ wsClient.send("ping");
|
|
43
|
+ }
|
|
44
|
+ }, 30000); // 30s
|
|
45
|
+ }
|
|
46
|
+}
|