guoshilong
2023-03-20 eb70b82c29aa08cd3e94b1f6c0ced5f1cd7135dd
src/main/java/org/springblade/common/handler/SocketIOService.java
@@ -50,16 +50,8 @@
   @Autowired
   private SocketIOServer socketIOServer;
   @Autowired
   private IFunctionService functionService;
   @Autowired
   private IModulesService modulesService;
   /**
    * Spring IoC容器创建之后,在加载SocketIOConfiguration Bean之后启动
    *
    * @throws Exception
    */
   @PostConstruct
   private void autoStartup() throws Exception {
@@ -68,8 +60,6 @@
   /**
    * Spring IoC容器在销毁SocketIOConfiguration Bean之前关闭,避免重启项目服务端口占用问题
    *
    * @throws Exception
    */
   @PreDestroy
   private void autoStop() throws Exception {
@@ -83,12 +73,20 @@
   /**
    * 连接时
    *
    * @param client
    */
   @OnConnect
   public void onConnect(SocketIOClient client) {
      log.info(client.getSessionId().toString());
      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());
      }
   }
   /**
@@ -101,32 +99,6 @@
   @OnEvent(value = "msg")
   public void OnEvent(SocketIOClient client, AckRequest ackRequest, String data) {
      log.info("发来消息:" + data);
   }
   /**
    * 添加进clientMap
    */
   @OnEvent(value = "putInClientMap")
   public void OnEventPutInClientMap(SocketIOClient client, AckRequest ackRequest, String data) {
      FunctionEntity functionEntity = new FunctionEntity();
      SocketEntity socketEntity = parseMsg(data);
      //判断是否是控制器
      if (StringUtil.isBlank(socketEntity.getTarget())) {
         //媒体端
         functionEntity.setModulesId(socketEntity.getCurrent());
      } else {
         //控制器
         functionEntity.setModulesId(socketEntity.getTarget());
      }
      List<FunctionEntity> all = functionService.getAll(functionEntity);
      if (all.size() > 0) {
         clientMap.put(socketEntity.getCurrent(), client);
         client.sendEvent("connectOk", R.data(all));
      } else {
         client.sendEvent("connectError", R.fail("该模块不存在"));
      }
   }
   /**
@@ -152,7 +124,7 @@
   public void OnEventMenuChange(SocketIOClient client, AckRequest ackRequest, String data) {
      log.info("菜单变更:" + data);
      SocketEntity socketEntity = parseMsg(data);
      log.info("目标客户端:{}", clientMap.get(socketEntity.getTarget()).getSessionId().toString());
      log.info("控制的设备是:{}", parseMsg(client.getHandshakeData().getSingleUrlParam("connectInfo")).getTarget());
      clientMap.get(socketEntity.getTarget()).sendEvent("menuChange", socketEntity.getMsg());
   }
@@ -163,12 +135,14 @@
    */
   @OnDisconnect()
   public void OnEvent(SocketIOClient client) {
      clientMap.forEach((e, socketIOClient) -> {
         if (socketIOClient.equals(client)) {
            clientMap.remove(e);
            log.info("{}断开连接", e);
      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() {
@@ -207,7 +181,7 @@
   }
   /**
    * 解析客户端连接请求中的参数,转化为实体类
    * 解析参数,转化为实体类
    */
   private SocketEntity parseMsg(String msg) {
      return JSON.parseObject(msg, SocketEntity.class);