From 51e71fd8dd1439c631ea1562d91de8882296fa9d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E5=87=A4=E5=90=89?= Date: Sat, 26 Apr 2025 16:51:38 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=A2=9E=E5=8A=A0ws=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle | 25 ++++++++++++---------- .../gd/system/model/dto/WebsocketResult.java | 18 ++++++++++++++++ .../gd/system/service/WebSocketService.java | 18 ++++++++++++++++ 3 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 src/main/java/com/iflytop/gd/system/model/dto/WebsocketResult.java create mode 100644 src/main/java/com/iflytop/gd/system/service/WebSocketService.java diff --git a/build.gradle b/build.gradle index ea27ea2..5f6af88 100644 --- a/build.gradle +++ b/build.gradle @@ -18,26 +18,29 @@ repositories { } dependencies { + implementation 'ch.qos.logback:logback-core:1.5.16' + implementation 'cn.hutool:hutool-all:5.8.35' + implementation 'com.baomidou:mybatis-plus-boot-starter:3.5.10.1' + implementation 'com.baomidou:mybatis-plus-generator:3.5.10.1' + implementation 'com.baomidou:mybatis-plus-jsqlparser:3.5.10.1' + implementation 'com.github.xiaoymin:knife4j-openapi3-jakarta-spring-boot-starter:4.5.0' + implementation 'jakarta.annotation:jakarta.annotation-api:2.1.1' + implementation 'org.freemarker:freemarker:2.3.34' + implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.4' implementation 'org.springframework.boot:spring-boot-starter' implementation 'org.springframework.boot:spring-boot-starter-web' - implementation group: 'ch.qos.logback', name: 'logback-core', version: '1.5.16' - implementation group: 'org.xerial', name: 'sqlite-jdbc', version: '3.48.0.0' - implementation 'org.mybatis.spring.boot:mybatis-spring-boot-starter:3.0.4' - implementation group: 'org.freemarker', name: 'freemarker', version: '2.3.34' - implementation group: 'com.baomidou', name: 'mybatis-plus-boot-starter', version: '3.5.10.1' - implementation group: 'com.baomidou', name: 'mybatis-plus-jsqlparser', version: '3.5.10.1' - implementation group: 'com.baomidou', name: 'mybatis-plus-generator', version: '3.5.10.1' - implementation group: 'cn.hutool', name: 'hutool-all', version: '5.8.35' - implementation group: 'com.github.xiaoymin', name: 'knife4j-openapi3-jakarta-spring-boot-starter', version: '4.5.0' - implementation group: 'org.springframework.boot', name: 'spring-boot-starter-websocket', version: '3.4.2' - implementation 'jakarta.annotation:jakarta.annotation-api:2.1.1' + implementation 'org.springframework.boot:spring-boot-starter-websocket:3.4.2' + implementation 'org.springframework.statemachine:spring-statemachine-core:4.0.0' + implementation 'org.xerial:sqlite-jdbc:3.48.0.0' compileOnly 'org.projectlombok:lombok' annotationProcessor 'org.projectlombok:lombok' + testImplementation 'org.springframework.boot:spring-boot-starter-test' testRuntimeOnly 'org.junit.platform:junit-platform-launcher' } + tasks.named('test') { useJUnitPlatform() } diff --git a/src/main/java/com/iflytop/gd/system/model/dto/WebsocketResult.java b/src/main/java/com/iflytop/gd/system/model/dto/WebsocketResult.java new file mode 100644 index 0000000..020abd5 --- /dev/null +++ b/src/main/java/com/iflytop/gd/system/model/dto/WebsocketResult.java @@ -0,0 +1,18 @@ +package com.iflytop.gd.system.model.dto; + +import io.swagger.v3.oas.annotations.media.Schema; +import lombok.Data; + +@Data +public class WebsocketResult { + /** + * 推送类型(指令 cmd,报警 warn ,状态 status) + */ + @Schema(description = "推送类型(指令 cmd,报警 warn ,状态 status)") + private String type; + /** + * 执行结果 + */ + @Schema(description = "推送数据") + private Object data; +} \ No newline at end of file diff --git a/src/main/java/com/iflytop/gd/system/service/WebSocketService.java b/src/main/java/com/iflytop/gd/system/service/WebSocketService.java new file mode 100644 index 0000000..d1421d6 --- /dev/null +++ b/src/main/java/com/iflytop/gd/system/service/WebSocketService.java @@ -0,0 +1,18 @@ +package com.iflytop.gd.system.service; + +import cn.hutool.json.JSONUtil; +import com.iflytop.gd.system.config.WebSocketServer; +import com.iflytop.gd.system.model.dto.WebsocketResult; +import org.springframework.stereotype.Service; + +@Service +public class WebSocketService { + public void pushMsg(String type, Object result) { + + WebsocketResult websocketResult = new WebsocketResult(); + websocketResult.setType(type); + websocketResult.setData(result); + // 发送消息给客户端 + WebSocketServer.sendMessageToClients(JSONUtil.toJsonStr(websocketResult)); + } +}