From ac2b4ef91a2dc4ad452f8fadf2e9fc0bcb23ac7d Mon Sep 17 00:00:00 2001
From: guoshilong <123456>
Date: Thu, 23 Mar 2023 11:53:10 +0800
Subject: [PATCH] 修复问题
---
src/main/java/org/springblade/common/handler/SocketIOService.java | 132 ++++++++++++++++++++++++++++----------------
1 files changed, 84 insertions(+), 48 deletions(-)
diff --git a/src/main/java/org/springblade/common/handler/SocketIOService.java b/src/main/java/org/springblade/common/handler/SocketIOService.java
index b6c88ec..d7f8d81 100644
--- a/src/main/java/org/springblade/common/handler/SocketIOService.java
+++ b/src/main/java/org/springblade/common/handler/SocketIOService.java
@@ -1,11 +1,20 @@
package org.springblade.common.handler;
+import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.corundumstudio.socketio.*;
import com.corundumstudio.socketio.annotation.OnConnect;
import com.corundumstudio.socketio.annotation.OnDisconnect;
import com.corundumstudio.socketio.annotation.OnEvent;
import com.corundumstudio.socketio.protocol.Packet;
+import org.springblade.core.mp.support.Condition;
+import org.springblade.core.tool.api.R;
+import org.springblade.core.tool.utils.StringUtil;
+import org.springblade.modules.modules.entity.FunctionEntity;
+import org.springblade.modules.modules.entity.ModulesEntity;
+import org.springblade.modules.modules.service.IFunctionService;
+import org.springblade.modules.modules.service.IModulesService;
+import org.springblade.modules.modules.vo.FunctionVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.core.annotation.Order;
@@ -19,6 +28,8 @@
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import java.net.ServerSocket;
+import java.util.ArrayList;
+import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -34,27 +45,21 @@
private final Logger log = LoggerFactory.getLogger(this.getClass());
- // 用来存已连接的客户端
private static Map<String, SocketIOClient> clientMap = new ConcurrentHashMap<>();
@Autowired
private SocketIOServer socketIOServer;
/**
- * Spring IoC容器创建之后,在加载SocketIOServiceImpl Bean之后启动
- *
- * @throws Exception
+ * Spring IoC容器创建之后,在加载SocketIOConfiguration Bean之后启动
*/
@PostConstruct
private void autoStartup() throws Exception {
- log.info("启动Socket!!!!!!!!!!");
start();
}
/**
- * Spring IoC容器在销毁SocketIOServiceImpl Bean之前关闭,避免重启项目服务端口占用问题
- *
- * @throws Exception
+ * Spring IoC容器在销毁SocketIOConfiguration Bean之前关闭,避免重启项目服务端口占用问题
*/
@PreDestroy
private void autoStop() throws Exception {
@@ -62,59 +67,83 @@
}
public void start() {
- // 监听客户端连接
- socketIOServer.addConnectListener(client -> {
- String uid = getParamsByClient(client);
- if (uid != null) {
- clientMap.put(uid, client);
- log.info("有新的客户端连接UID:{}", uid);
- }
- // 给客户端发送一条信息 发送ClientReceive事件 需要客户端绑定此事件即可接收到消息
- JSONObject jsonObject = new JSONObject();
- jsonObject.put("name", "goat");
- jsonObject.put("message", "hello client");
- client.sendEvent("ClientReceive", jsonObject);
- });
-
- // 监听客户端断开连接
- socketIOServer.addDisconnectListener(listener -> {
- String uid = getParamsByClient(listener);
- if (uid != null) {
- clientMap.remove(uid);
- listener.disconnect();
- log.info("一条客户端连接中断");
- }
- });
-
- socketIOServer.addEventListener("ServerReceive", JSONObject.class, (client, data, ackSender) -> {
- String uid = getParamsByClient(client);
- String ip = getIpByClient(client);
- if (uid != null) {
- log.info("接收到SID:{}发来的消息:{}", uid, data.toJSONString());
- log.debug(ip + " ************ 客户端:" + data);
- }
- });
-
socketIOServer.start();
log.info("socket.io初始化服务完成");
}
+ /**
+ * 连接时
+ */
@OnConnect
- public void onConnect(SocketIOClient client){
- log.info(getIpByClient(client));
- HandshakeData handshakeData = client.getHandshakeData();
- Map<String, List<String>> urlParams = handshakeData.getUrlParams();
+ public void onConnect(SocketIOClient client) {
+ String connectInfo = client.getHandshakeData().getSingleUrlParam("connectInfo");
+ SocketEntity socketEntity = parseMsg(connectInfo);
+ if (clientMap.containsKey(socketEntity.getCurrent())){
+ client.sendEvent("connectError",R.fail("连接已存在"));
+ log.info("设备{}连接已存在",socketEntity.getCurrent());
+ }else {
+ clientMap.put(socketEntity.getCurrent(),client);
+ client.sendEvent("connectOk",R.success("连接成功"));
+ log.info("设备{}连接成功",socketEntity.getCurrent());
+ }
}
+ /**
+ * 监听前端订阅相同事件发送过来的信息
+ *
+ * @param client
+ * @param ackRequest
+ * @param data
+ */
@OnEvent(value = "msg")
- public void OnEvent(SocketIOClient client, AckRequest ackRequest, String data){
+ public void OnEvent(SocketIOClient client, AckRequest ackRequest, String data) {
log.info("发来消息:" + data);
}
+ /**
+ * 图册上下页控制
+ */
+ @OnEvent(value = "changeImgPage")
+ public void OnEventImgChange(SocketIOClient client, AckRequest ackRequest, String data) {
+ log.info("图册变更:" + data);
+ SocketEntity socketEntity = parseMsg(data);
+ if (socketEntity.getMsg().equals("previous")) {
+ //上一页
+ clientMap.get(socketEntity.getTarget()).sendEvent("previousPage", "previous");
+ } else {
+ //下一页
+ clientMap.get(socketEntity.getTarget()).sendEvent("nextPage", "next");
+ }
+ }
+
+ /**
+ * 菜单控制
+ */
+ @OnEvent(value = "menuChange")
+ public void OnEventMenuChange(SocketIOClient client, AckRequest ackRequest, String data) {
+ log.info("菜单变更:" + data);
+ SocketEntity socketEntity = parseMsg(data);
+ log.info("控制的设备是:{}", parseMsg(client.getHandshakeData().getSingleUrlParam("connectInfo")).getTarget());
+ clientMap.get(socketEntity.getTarget()).sendEvent("menuChange", socketEntity.getMsg());
+ }
+
+ /**
+ * 断开连接时
+ *
+ * @param client
+ */
@OnDisconnect()
- public void OnEvent(SocketIOClient client){
- log.info("{}断开连接",client.getSessionId());
+ public void OnEvent(SocketIOClient client) {
+ String connectInfo = client.getHandshakeData().getSingleUrlParam("connectInfo");
+ SocketEntity socketEntity = parseMsg(connectInfo);
+ //保证断连只会断连自己,不会断连其他客户端
+ clientMap.forEach((key,value)->{
+ if (value.getSessionId().equals(client.getSessionId())){
+ clientMap.remove(key);
+ }
+ });
+ log.info("设备{}断开连接",socketEntity.getCurrent());
}
public void stop() {
@@ -152,5 +181,12 @@
return sa.substring(1, sa.indexOf(":"));
}
+ /**
+ * 解析参数,转化为实体类
+ */
+ private SocketEntity parseMsg(String msg) {
+ return JSON.parseObject(msg, SocketEntity.class);
+ }
+
}
--
Gitblit v1.9.3