guoshilong
2023-03-18 ac53ebf1b33930f175cf59c26c2a92bbb25366db
src/main/java/org/springblade/common/handler/SocketIOService.java
@@ -7,9 +7,12 @@
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.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;
@@ -41,19 +44,25 @@
   private final Logger log = LoggerFactory.getLogger(this.getClass());
   // 用来存已连接的客户端
   private static Map<String, SocketIOClient> clientMap = new ConcurrentHashMap<>();
   private static Map<String, SocketIOClient> viewMap = new ConcurrentHashMap<>();
   // 用来存已连接的视图客户端
   private List<Map<String, SocketIOClient>> view = new ArrayList<>();
   // 用来存已连接的客户端
   private static Map<String, SocketIOClient> controllerMap = new ConcurrentHashMap<>();
   // 用来存已连接的控制客户端
   private List<Map<String, SocketIOClient>> controller = new ArrayList<>();
//   // 用来存已连接的视图客户端
//   private List<Map<String, SocketIOClient>> view = new ArrayList<>();
//
//   // 用来存已连接的控制客户端
//   private List<Map<String, SocketIOClient>> controller = new ArrayList<>();
   @Autowired
   private SocketIOServer socketIOServer;
   @Autowired
   private IFunctionService functionService;
   @Autowired
   private IModulesService modulesService;
   /**
    * Spring IoC容器创建之后,在加载SocketIOConfiguration Bean之后启动
@@ -82,6 +91,7 @@
   /**
    * 连接时
    *
    * @param client
    */
   @OnConnect
@@ -90,15 +100,15 @@
      //判断页面和控制器是否已经连接过了
      if (functionEntity.getIsView()){
         for (int i = 0; i < view.size(); i++) {
            if (view.get(i).containsKey(functionEntity.getModulesId())){
         for (int i = 0; i < viewMap.size(); i++) {
            if (viewMap.containsKey(functionEntity.getModulesId())) {
               client.sendEvent("connectError",R.fail("已连接"));
               return;
            }
         }
      }else {
         for (int i = 0; i < controller.size(); i++) {
            if (controller.get(i).containsKey(functionEntity.getModulesId())){
         for (int i = 0; i < controllerMap.size(); i++) {
            if (controllerMap.containsKey(functionEntity.getModulesId())) {
               client.sendEvent("connectError",R.fail("已连接"));
               return;
            }
@@ -106,13 +116,14 @@
      }
      List<FunctionEntity> all = functionService.getAll(functionEntity);
      log.info(client.getSessionId().toString());
      if (all.size()>0){
         clientMap.put(functionEntity.getModulesId(),client);
         if (functionEntity.getIsView()){
               view.add(clientMap);
            log.info("预览页{}", client.getSessionId().toString());
            viewMap.put(functionEntity.getModulesId(), client);
         }else {
               controller.add(clientMap);
            log.info("控制页{}", client.getSessionId().toString());
            controllerMap.put(functionEntity.getModulesId(), client);
         }
         client.sendEvent("connectOk", R.data(all));
      }else {
@@ -122,6 +133,7 @@
   /**
    * 监听前端订阅相同事件发送过来的信息
    *
    * @param client
    * @param ackRequest
    * @param data
@@ -132,56 +144,65 @@
   }
   /**
    * 监听前端订阅相同事件发送过来的信息
    * @param client
    * @param ackRequest
    * @param data
    * 图册上下页控制
    */
   @OnEvent(value = "changeImgPage")
   public void OnEventImgChange(SocketIOClient client, AckRequest ackRequest, String data){
      log.info("图册变更:" + data);
      FunctionVO funcVo = getFuncVo(client);
      view.forEach(e->{
      viewMap.forEach((e,socketIO)->{
         if (data.equals("previous")){
            e.get(funcVo.getModulesId()).sendEvent("previousPage","previous");
            if (e.equals(funcVo.getModulesId())){
               socketIO.sendEvent("previousPage","previous");
            }
         }else if (data.equals("next")){
            e.get(funcVo.getModulesId()).sendEvent("nextPage","next");
            if (e.equals(funcVo.getModulesId())){
               socketIO.sendEvent("previousPage","next");
            }
         }
      });
   }
   /**
    * 菜单控制
    */
   @OnEvent(value = "menuChange")
   public void OnEventMenuChange(SocketIOClient client, AckRequest ackRequest, String data){
      log.info("菜单变更:" + data);
      FunctionVO funcVo = getFuncVo(client);
      view.forEach(e->{
         e.get(funcVo.getModulesId()).sendEvent("menuChange",data);
      viewMap.forEach((e,socketIOClient)->{
         if (e.equals(funcVo.getModulesId())){
            log.info("目标客户端:{}", socketIOClient.getSessionId().toString());
            socketIOClient.sendEvent("menuChange", data);
         }
      });
   }
   /**
    * 断开连接时
    *
    * @param client
    */
   @OnDisconnect()
   public void OnEvent(SocketIOClient client){
      FunctionVO funcVo = getFuncVo(client);
      if (funcVo.getIsView()){
         for (int i = 0; i <=view.size() ; i++) {
            if (view.get(i).containsKey(funcVo.getModulesId())) {
               view.remove(i);
         viewMap.forEach((e,socketIOClient)->{
            if (e.equals(funcVo.getModulesId())){
               viewMap.remove(e);
               log.info("预览页:{}断开连接", socketIOClient.getSessionId());
            }
         }
         });
      }else {
         for (int i = 0; i <=controller.size() ; i++) {
            if (controller.get(i).containsKey(funcVo.getModulesId())) {
               controller.remove(i);
         controllerMap.forEach((e,socketIOClient)->{
            if (e.equals(funcVo.getModulesId())){
               controllerMap.remove(e);
               log.info("控制页:{}断开连接", socketIOClient.getSessionId());
            }
         });
         }
      }
      log.info("{}断开连接",client.getSessionId());
   }
   public void stop() {