From a7395ae913cc3b0c6be65870e717ff536d9008b0 Mon Sep 17 00:00:00 2001 From: yang_shj <1069818635@QQ.com> Date: Thu, 14 Sep 2023 08:22:53 +0800 Subject: [PATCH] =?UTF-8?q?#=E5=91=8A=E8=AD=A6websocket=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../alarm/config/ws/hz3000/SystemAlarmRegular.java | 51 --------- .../config/ws/hz3000/SystemAlarmWebSocket.java | 125 --------------------- .../hzims/alarm/ws/hz3000/SystemAlarmRegular.java | 51 +++++++++ .../alarm/ws/hz3000/SystemAlarmWebSocket.java | 125 +++++++++++++++++++++ 4 files changed, 176 insertions(+), 176 deletions(-) delete mode 100644 hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/config/ws/hz3000/SystemAlarmRegular.java delete mode 100644 hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/config/ws/hz3000/SystemAlarmWebSocket.java create mode 100644 hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/hz3000/SystemAlarmRegular.java create mode 100644 hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/hz3000/SystemAlarmWebSocket.java diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/config/ws/hz3000/SystemAlarmRegular.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/config/ws/hz3000/SystemAlarmRegular.java deleted file mode 100644 index e0ae528..0000000 --- a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/config/ws/hz3000/SystemAlarmRegular.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.hnac.hzims.alarm.config.ws.hz3000; - -import com.hnac.hzims.alarm.show.service.SystemAlarmService; -import lombok.extern.slf4j.Slf4j; -import org.springblade.core.tool.utils.ObjectUtil; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.scheduling.annotation.EnableScheduling; -import org.springframework.scheduling.annotation.Scheduled; -import org.springframework.stereotype.Component; - -import java.net.URI; - -/** - * 等级告警获取数据长链接 - * @author ysj - */ -@Slf4j -@Component -@EnableScheduling -public class SystemAlarmRegular { - - @Value("${hzims.system.alarm-url}") - private String system_alarm_url; - - private SystemAlarmWebSocket client; - - @Autowired - private SystemAlarmService systemAlarmService; - - // 定时发送消息 - @Scheduled(cron = "0 0/30 * * * ?") - private void regular(){ - // 检查链接存活状态 - if(ObjectUtil.isNotEmpty(client) && client.isOpen()){ - client.send(systemAlarmService.sendMessage()); - }else { - this.createClient(); - } - } - - // 创建websocket链接 - private void createClient() { - try{ - client = new SystemAlarmWebSocket(new URI(system_alarm_url)); - client.connectBlocking(); - }catch (Exception e){ - log.error("level create error : {}",e.getMessage()); - } - } -} \ No newline at end of file diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/config/ws/hz3000/SystemAlarmWebSocket.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/config/ws/hz3000/SystemAlarmWebSocket.java deleted file mode 100644 index 92e5e79..0000000 --- a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/config/ws/hz3000/SystemAlarmWebSocket.java +++ /dev/null @@ -1,125 +0,0 @@ -package com.hnac.hzims.alarm.config.ws.hz3000; - -import com.hnac.hzims.alarm.show.service.SystemAlarmService; -import lombok.extern.slf4j.Slf4j; -import org.java_websocket.client.WebSocketClient; -import org.java_websocket.handshake.ServerHandshake; -import org.springblade.core.tool.utils.SpringUtil; -import org.springblade.core.tool.utils.StringUtil; - -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; -import java.net.Socket; -import java.net.URI; -import java.security.SecureRandom; -import java.security.cert.X509Certificate; -import java.util.Optional; - -/** - * 等级告警获取数据长链接 - * @author ysj - */ -@Slf4j -public class SystemAlarmWebSocket extends WebSocketClient { - - private final SystemAlarmService systemAlarmService; - - /** - * 构造等级告警websocket - * @param uri - */ - public SystemAlarmWebSocket(URI uri) { - super(uri); - systemAlarmService = SpringUtil.getBean(SystemAlarmService.class); - connection(this); - } - - // 链接到服务器回调接口 - @Override - public void onOpen(ServerHandshake handshakedata) { - log.error("systemAlarm websocket open"); - } - - // 接收到服务器消息回调接口 - @Override - public void onMessage(String message) { - if(StringUtil.isEmpty(message)){ - log.error("systemAlarm on message is null"); - }else{ - // 等级告警数据处理 - systemAlarmService.receiveMessage(message); - } - } - - // 与服务器链接中断回调接口 - @Override - public void onClose(int code, String reason, boolean remote) { - log.error("systemAlarm websocket close"); - } - - // 与服务器通讯异常触发 - @Override - public void onError(Exception e) { - log.error("systemAlarm websocket error : {}",e.getMessage()); - } - - /** - * 建立链接 - * @param webSocket - */ - private void connection(SystemAlarmWebSocket webSocket) { - SSLContext context = init(); - if(Optional.ofNullable(context).isPresent()){ - Socket socket = create(context); - if(Optional.ofNullable(socket).isPresent()){ - webSocket.setSocket(socket); - } - } - } - - /** - * 创建Socket - * @param context - * @return - */ - private Socket create(SSLContext context) { - Socket socket = null; - try{ - socket = context.getSocketFactory().createSocket(); - }catch (Exception e){ - log.error("systemAlarm socket create error : {}",e.getMessage()); - } - return socket; - } - - /** - * 协议初始化 - * @return SSLContext - */ - private SSLContext init() { - SSLContext SSL = null; - try{ - SSL = SSLContext.getInstance("TLS"); - SSL.init(null, new TrustManager[]{new X509TrustManager() { - @Override - public void checkClientTrusted(X509Certificate[] chain, - String authType) { - } - - @Override - public void checkServerTrusted(X509Certificate[] chain, String authType) { - } - - @Override - public X509Certificate[] getAcceptedIssuers() { - return new X509Certificate[0]; - } - }}, new SecureRandom()); - }catch (Exception e){ - log.error("systemAlarm SSL init error : {}",e.getMessage()); - } - return SSL; - } - -} \ No newline at end of file diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/hz3000/SystemAlarmRegular.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/hz3000/SystemAlarmRegular.java new file mode 100644 index 0000000..72008ad --- /dev/null +++ b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/hz3000/SystemAlarmRegular.java @@ -0,0 +1,51 @@ +package com.hnac.hzims.alarm.ws.hz3000; + +import com.hnac.hzims.alarm.show.service.SystemAlarmService; +import lombok.extern.slf4j.Slf4j; +import org.springblade.core.tool.utils.ObjectUtil; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.stereotype.Component; + +import java.net.URI; + +/** + * 等级告警获取数据长链接 + * @author ysj + */ +@Slf4j +@Component +@EnableScheduling +public class SystemAlarmRegular { + + @Value("${hzims.system.alarm-url}") + private String system_alarm_url; + + private SystemAlarmWebSocket client; + + @Autowired + private SystemAlarmService systemAlarmService; + + // 定时发送消息 + @Scheduled(cron = "0 0/30 * * * ?") + private void regular(){ + // 检查链接存活状态 + if(ObjectUtil.isNotEmpty(client) && client.isOpen()){ + client.send(systemAlarmService.sendMessage()); + }else { + this.createClient(); + } + } + + // 创建websocket链接 + private void createClient() { + try{ + client = new SystemAlarmWebSocket(new URI(system_alarm_url)); + client.connectBlocking(); + }catch (Exception e){ + log.error("level create error : {}",e.getMessage()); + } + } +} \ No newline at end of file diff --git a/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/hz3000/SystemAlarmWebSocket.java b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/hz3000/SystemAlarmWebSocket.java new file mode 100644 index 0000000..f937f50 --- /dev/null +++ b/hzims-service/hzims-alarm/src/main/java/com/hnac/hzims/alarm/ws/hz3000/SystemAlarmWebSocket.java @@ -0,0 +1,125 @@ +package com.hnac.hzims.alarm.ws.hz3000; + +import com.hnac.hzims.alarm.show.service.SystemAlarmService; +import lombok.extern.slf4j.Slf4j; +import org.java_websocket.client.WebSocketClient; +import org.java_websocket.handshake.ServerHandshake; +import org.springblade.core.tool.utils.SpringUtil; +import org.springblade.core.tool.utils.StringUtil; + +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; +import java.net.Socket; +import java.net.URI; +import java.security.SecureRandom; +import java.security.cert.X509Certificate; +import java.util.Optional; + +/** + * 等级告警获取数据长链接 + * @author ysj + */ +@Slf4j +public class SystemAlarmWebSocket extends WebSocketClient { + + private final SystemAlarmService systemAlarmService; + + /** + * 构造等级告警websocket + * @param uri + */ + public SystemAlarmWebSocket(URI uri) { + super(uri); + systemAlarmService = SpringUtil.getBean(SystemAlarmService.class); + connection(this); + } + + // 链接到服务器回调接口 + @Override + public void onOpen(ServerHandshake handshakedata) { + log.error("systemAlarm websocket open"); + } + + // 接收到服务器消息回调接口 + @Override + public void onMessage(String message) { + if(StringUtil.isEmpty(message)){ + log.error("systemAlarm on message is null"); + }else{ + // 等级告警数据处理 + systemAlarmService.receiveMessage(message); + } + } + + // 与服务器链接中断回调接口 + @Override + public void onClose(int code, String reason, boolean remote) { + log.error("systemAlarm websocket close"); + } + + // 与服务器通讯异常触发 + @Override + public void onError(Exception e) { + log.error("systemAlarm websocket error : {}",e.getMessage()); + } + + /** + * 建立链接 + * @param webSocket + */ + private void connection(SystemAlarmWebSocket webSocket) { + SSLContext context = init(); + if(Optional.ofNullable(context).isPresent()){ + Socket socket = create(context); + if(Optional.ofNullable(socket).isPresent()){ + webSocket.setSocket(socket); + } + } + } + + /** + * 创建Socket + * @param context + * @return + */ + private Socket create(SSLContext context) { + Socket socket = null; + try{ + socket = context.getSocketFactory().createSocket(); + }catch (Exception e){ + log.error("systemAlarm socket create error : {}",e.getMessage()); + } + return socket; + } + + /** + * 协议初始化 + * @return SSLContext + */ + private SSLContext init() { + SSLContext SSL = null; + try{ + SSL = SSLContext.getInstance("TLS"); + SSL.init(null, new TrustManager[]{new X509TrustManager() { + @Override + public void checkClientTrusted(X509Certificate[] chain, + String authType) { + } + + @Override + public void checkServerTrusted(X509Certificate[] chain, String authType) { + } + + @Override + public X509Certificate[] getAcceptedIssuers() { + return new X509Certificate[0]; + } + }}, new SecureRandom()); + }catch (Exception e){ + log.error("systemAlarm SSL init error : {}",e.getMessage()); + } + return SSL; + } + +} \ No newline at end of file