Merge remote-tracking branch 'origin/master'

dev
zhouhy 1 year ago
commit 389e333fe0

@ -0,0 +1,61 @@
package com.ruoyi.business.mapper;
import java.util.List;
import com.ruoyi.business.domain.HwDeviceModeFunction;
/**
* Mapper
*
* @author xins
* @date 2023-09-05
*/
public interface HwDeviceModeFunctionMapper
{
/**
*
*
* @param modeFunctionId
* @return
*/
public HwDeviceModeFunction selectHwDeviceModeFunctionByModeFunctionId(Long modeFunctionId);
/**
*
*
* @param hwDeviceModeFunction
* @return
*/
public List<HwDeviceModeFunction> selectHwDeviceModeFunctionList(HwDeviceModeFunction hwDeviceModeFunction);
/**
*
*
* @param hwDeviceModeFunction
* @return
*/
public int insertHwDeviceModeFunction(HwDeviceModeFunction hwDeviceModeFunction);
/**
*
*
* @param hwDeviceModeFunction
* @return
*/
public int updateHwDeviceModeFunction(HwDeviceModeFunction hwDeviceModeFunction);
/**
*
*
* @param modeFunctionId
* @return
*/
public int deleteHwDeviceModeFunctionByModeFunctionId(Long modeFunctionId);
/**
*
*
* @param modeFunctionIds
* @return
*/
public int deleteHwDeviceModeFunctionByModeFunctionIds(Long[] modeFunctionIds);
}

@ -0,0 +1,65 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<groupId>com.ruoyi</groupId>
<artifactId>ruoyi-modules</artifactId>
<version>3.6.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<!-- <artifactId>hw-mqtt-broker</artifactId>-->
<artifactId>ruoyi-modules-websocket</artifactId>
<description>
海威物联网平台WebSocket模块
</description>
<properties>
<java.version>8</java.version>
</properties>
<dependencies>
<!-- SpringCloud Alibaba Nacos discovery -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>
<!-- SpringCloud Alibaba Nacos Config -->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>
<!-- SpringCloud Alibaba Sentinel -->
<!-- <dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-sentinel</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>

@ -0,0 +1,14 @@
package com.hw.websocket;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
public class HwWebsocketServerApplication {
public static void main(String[] args) {
SpringApplication.run(HwWebsocketServerApplication.class, args);
}
}

@ -0,0 +1,23 @@
package com.hw.websocket.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
/**
* @author Wen JY
* @description: TODO
* @date 2023-09-14 16:22:13
* @version: 1.0
*/
@Configuration
public class WebSocketConfig {
/**
* ServerEndpointExporter
* bean使@ServerEndpointWebsocket endpoint
*/
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}

@ -0,0 +1,39 @@
package com.hw.websocket.controller;
import com.alibaba.fastjson2.JSONObject;
import com.hw.websocket.operate.WebSocket;
import com.ruoyi.common.core.utils.uuid.UUID;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
/**
* @author Wen JY
* @description: TODO
* @date 2023-09-14 17:07:06
* @version: 1.0
*/
@Controller
@RequestMapping("/webSocketApi")
public class WebSocketApi {
private Logger logger = LoggerFactory.getLogger(WebSocketApi.class);
@Resource
private WebSocket webSocket;
@PostMapping()
@ResponseBody
public void push(String message){
try{
webSocket.sendAllMessage(message);
}catch (Exception ex){
logger.error("WebSocket Api接口异常:"+ex.getMessage());
}
}
}

@ -0,0 +1,134 @@
package com.hw.websocket.operate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import javax.websocket.*;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
/**
* @author Wen JY
* @description: TODO WebSocket
* @date 2023-09-14 16:32:43
* @version: 1.0
*/
@Component
@ServerEndpoint("/websocket/{userId}") // 接口路径 ws://localhost:8087/webSocket/userId;
public class WebSocket {
private Logger logger = LoggerFactory.getLogger(WebSocket.class);
//与某个客户端的连接会话,需要通过它来给客户端发送数据
private Session session;
/**
* ID
*/
private String userId;
//concurrent包的线程安全Set用来存放每个客户端对应的MyWebSocket对象。
//虽然@Component默认是单例模式的但springboot还是会为每个websocket连接初始化一个bean所以可以用一个静态set保存起来。
// 注底下WebSocket是当前类名
private static CopyOnWriteArraySet<WebSocket> webSockets =new CopyOnWriteArraySet<>();
// 用来存在线连接用户信息
private static ConcurrentHashMap<String,Session> sessionPool = new ConcurrentHashMap<String,Session>();
/**
*
*/
@OnOpen
public void onOpen(Session session, @PathParam(value="userId")String userId) {
try {
this.session = session;
this.userId = userId;
webSockets.add(this);
sessionPool.put(userId, session);
logger.info("【websocket消息】有新的连接总数为:"+webSockets.size());
} catch (Exception e) {
}
}
/**
*
*/
@OnClose
public void onClose() {
try {
webSockets.remove(this);
sessionPool.remove(this.userId);
logger.info("【websocket消息】连接断开总数为:"+webSockets.size());
} catch (Exception e) {
}
}
/**
*
*
* @param message
* @param session
*/
@OnMessage
public void onMessage(String message) {
logger.info("【websocket消息】收到客户端消息:"+message);
}
/**
* @param session
* @param error
*/
@OnError
public void onError(Session session, Throwable error) {
logger.error("用户错误,原因:"+error.getMessage());
error.printStackTrace();
}
// 此为广播消息
public void sendAllMessage(String message) {
logger.info("【websocket消息】广播消息:"+message);
for(WebSocket webSocket : webSockets) {
try {
if(webSocket.session.isOpen()) {
webSocket.session.getAsyncRemote().sendText(message);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 此为单点消息
public void sendOneMessage(String userId, String message) {
Session session = sessionPool.get(userId);
if (session != null&&session.isOpen()) {
try {
logger.info("【websocket消息】 单点消息:"+message);
session.getAsyncRemote().sendText(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
// 此为单点消息(多人)
public void sendMoreMessage(String[] userIds, String message) {
for(String userId:userIds) {
Session session = sessionPool.get(userId);
if (session != null&&session.isOpen()) {
try {
logger.info("【websocket消息】 单点消息:"+message);
session.getAsyncRemote().sendText(message);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
}

@ -0,0 +1,25 @@
# Tomcat
server:
port: 9607
# Spring
spring:
application:
# ????
name: hw-websocket
profiles:
# ????
active: dev
cloud:
nacos:
discovery:
# ??????
server-addr: localhost:8848
config:
# ??????
server-addr: localhost:8848
# ??????
file-extension: yml
# ????
shared-configs:
- application-${spring.profiles.active}.${spring.cloud.nacos.config.file-extension}

@ -28,6 +28,7 @@
<module>hw-basic</module> <module>hw-basic</module>
<module>hw-tdengine</module> <module>hw-tdengine</module>
<module>hw-data-process</module> <module>hw-data-process</module>
<module>hw-websocket-server</module>
</modules> </modules>
<artifactId>ruoyi-modules</artifactId> <artifactId>ruoyi-modules</artifactId>

Loading…
Cancel
Save