فهرست منبع

修改:房间销毁规则,增加一个融云查询房主是否在房间的功能

hgw 3 سال پیش
والد
کامیت
237d6b40a9

+ 12 - 0
mec-biz/src/main/java/com/ym/mec/biz/service/impl/ImLiveBroadcastRoomServiceImpl.java

@@ -256,6 +256,10 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
         Date now = new Date();
         list.forEach(room -> {
             try {
+                //校验房间主播是否在房间 如果不在房间则进行销毁判断
+                if (imFeignService.userExistInRoom(room.getRoomUid(), room.getSpeakerId().toString())) {
+                    return;
+                }
                 destroyExpiredLiveRoom(now, room, expiredMinute);
             } catch (Exception e) {
                 log.error("roomDestroy>>>> failed roomId:{} msg:{}", room.getId(), e.getMessage());
@@ -736,6 +740,14 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
     public Map<String, Object> test(String roomUid, Integer userId) {
         //test
         Map<String, Object> result = new HashMap<>();
+        //主播在房间的状态
+        try {
+            boolean existInRoom = imFeignService.userExistInRoom(roomUid, userId.toString());
+            result.put("主播在房间", existInRoom ? "在" : "不在");
+        } catch (Exception e) {
+            result.put("主播在房间", "im无法连接");
+        }
+
         //点赞数
         Object like = redissonClient.getBucket(LIVE_ROOM_LIKE.replace(ROOM_UID, roomUid)).get();
         if (Objects.isNull(like)) {

+ 20 - 10
mec-client-api/src/main/java/com/ym/mec/im/ImFeignService.java

@@ -144,22 +144,22 @@ public interface ImFeignService {
     Object destroyLiveRoom(@RequestParam("roomId") String roomId);
 
     /**
-    * @description: 录制直播
      * @param roomId
-    * @return void
-    * @author zx
-    * @date 2022/2/25 13:52
-    */
+     * @return void
+     * @description: 录制直播
+     * @author zx
+     * @date 2022/2/25 13:52
+     */
     @PostMapping(value = "/liveRoom/startRecord")
     void startRecord(@RequestParam("roomId") String roomId);
 
     /**
-    * @description: 结束录制直播
      * @param roomId
-    * @return void
-    * @author zx
-    * @date 2022/2/25 13:52
-    */
+     * @return void
+     * @description: 结束录制直播
+     * @author zx
+     * @date 2022/2/25 13:52
+     */
     @PostMapping(value = "/liveRoom/stopRecord")
     void stopRecord(@RequestParam("roomId") String roomId);
 
@@ -169,4 +169,14 @@ public interface ImFeignService {
     @PostMapping(value = "/liveRoom/publishRoomMsg", consumes = MediaType.APPLICATION_JSON_VALUE)
     Object publishRoomMsg(@RequestBody ImRoomMessage message);
 
+    /**
+     * 查询用户是否在聊天室
+     *
+     * @param chatroomId 要查询的聊天室 ID(必传)
+     * @param userId     要查询的用户 ID(必传)
+     * @return true 在聊天室,false 不在聊天室
+     */
+    @PostMapping(value = "/liveRoom/userExistInRoom")
+    boolean userExistInRoom(@RequestParam("chatroomId") String chatroomId, @RequestParam("userId") String userId);
+
 }

+ 10 - 6
mec-client-api/src/main/java/com/ym/mec/im/fallback/ImFeignServiceFallback.java

@@ -1,19 +1,17 @@
 package com.ym.mec.im.fallback;
 
-import java.util.List;
-
 import com.ym.mec.common.entity.*;
-import org.springframework.stereotype.Component;
-
 import com.ym.mec.im.ImFeignService;
 import com.ym.mec.im.entity.GroupModel;
+import org.springframework.stereotype.Component;
+
+import java.util.List;
 
 @Component
 public class ImFeignServiceFallback implements ImFeignService {
     @Override
     public ImResult register(ImUserModel userModel) {
-    	
-    	System.out.println("*******************");
+        System.out.println("*******************");
         return null;
     }
 
@@ -101,4 +99,10 @@ public class ImFeignServiceFallback implements ImFeignService {
     public void stopRecord(String roomId) {
 
     }
+
+    @Override
+    public boolean userExistInRoom(String chatroomId, String userId) {
+        return false;
+    }
+
 }

+ 2 - 1
mec-im/src/main/java/com/ym/config/ResourceServerConfig.java

@@ -14,7 +14,8 @@ public class ResourceServerConfig extends ResourceServerConfigurerAdapter {
                         "/group/join", "/group/create", "/group/quit", "/room/leave", "/room/statusSync",
                         "/room/statusImMsg", "/group/batchDismiss", "/private/send", "/group/send",
                         "/group/dismiss", "/room/statusImMsg", "/history/get", "/user/statusImUser", "/liveRoom/recordSync",
-                        "/liveRoom/publishRoomMsg","/liveRoom/destroy","/liveRoom/create","/liveRoom/startRecord","/liveRoom/stopRecord")
+                        "/liveRoom/publishRoomMsg", "/liveRoom/destroy", "/liveRoom/create", "/liveRoom/startRecord",
+                        "/liveRoom/stopRecord", "/liveRoom/userExistInRoom")
                 .permitAll().anyRequest().authenticated().and().csrf().disable();
     }
 }

+ 7 - 0
mec-im/src/main/java/com/ym/controller/LiveRoomController.java

@@ -62,4 +62,11 @@ public class LiveRoomController {
     public void stopRecord(String roomId) throws Exception {
         liveRoomService.stopRecord(roomId);
     }
+
+    @ApiOperation("查询用户是否在聊天室")
+    @RequestMapping(value = "/userExistInRoom")
+    public boolean userExistInRoom(String chatroomId, String userId) {
+        return liveRoomService.userExistInRoom(chatroomId, userId);
+    }
+
 }

+ 26 - 0
mec-im/src/main/java/com/ym/mec/im/IMHelper.java

@@ -446,4 +446,30 @@ public class IMHelper {
 
         return (IMApiResultInfo) GsonUtil.fromJson(httpHelper.returnResult(conn), IMApiResultInfo.class);
     }
+
+    /**
+     * 查询用户是否在聊天室
+     *
+     * @param chatroomId 要查询的聊天室 ID(必传)
+     * @param userId     要查询的用户 ID(必传)
+     */
+    public IMApiResultInfo isInChartRoom(String chatroomId, String userId) throws Exception {
+        if (chatroomId == null) {
+            throw new BizException("房间Uid不能为空");
+        }
+        if (userId == null) {
+            throw new BizException("用户不能为空");
+        }
+        String body = "&chatroomId=" + URLEncoder.encode(chatroomId, UTF8) +
+                "&userId=" + URLEncoder.encode(userId, UTF8);
+        if (body.indexOf("&") == 0) {
+            body = body.substring(1);
+        }
+
+        HttpURLConnection conn = httpHelper.createIMPostHttpConnection("/chatroom/user/exist.json", "application/x-www-form-urlencoded");
+        httpHelper.setBodyParameter(body, conn);
+
+        return (IMApiResultInfo) GsonUtil.fromJson(httpHelper.returnResult(conn), IMApiResultInfo.class);
+    }
+
 }

+ 2 - 0
mec-im/src/main/java/com/ym/pojo/IMApiResultInfo.java

@@ -11,6 +11,8 @@ public class IMApiResultInfo {
     Integer code;
     // 错误信息。
     String errorMessage;
+    //人员是否存在 true 存在  false 不存在
+    Boolean isInChrm;
 
     public boolean isSuccess() {
         return code == 200;

+ 45 - 19
mec-im/src/main/java/com/ym/service/Impl/LiveRoomServiceImpl.java

@@ -13,7 +13,6 @@ import com.ym.pojo.RecordConfig;
 import com.ym.pojo.RecordNotify;
 import com.ym.service.LiveRoomService;
 import lombok.extern.slf4j.Slf4j;
-import org.apache.commons.lang3.StringUtils;
 import org.redisson.api.RBucket;
 import org.redisson.api.RedissonClient;
 import org.springframework.beans.factory.annotation.Autowired;
@@ -24,6 +23,7 @@ import java.util.ArrayList;
 import java.util.Date;
 import java.util.List;
 import java.util.Objects;
+import java.util.concurrent.TimeUnit;
 
 /**
  * @author hgw
@@ -167,10 +167,11 @@ public class LiveRoomServiceImpl implements LiveRoomService {
                     if (Objects.isNull(video)) {
                         return;
                     }
-                    video.setEndTime(new Date());
+                    Date now = new Date();
+                    video.setEndTime(now);
                     video.setType(2);
                     video.setUrl(fileUrl);
-                    video.setCreatedTime(new Date());
+                    video.setCreatedTime(now);
                     imLiveRoomVideoService.updateById(video);
                 } catch (Exception e) {
                     log.error("recordSync >>>>  error : {}", e.getMessage());
@@ -179,32 +180,57 @@ public class LiveRoomServiceImpl implements LiveRoomService {
         }
     }
 
+    /**
+     * 查询用户是否在聊天室
+     *
+     * @param chatroomId 要查询的聊天室 ID(必传)
+     * @param userId     要查询的用户 ID(必传)
+     * @return true 在聊天室,false 不在聊天室
+     */
+    public boolean userExistInRoom(String chatroomId, String userId) {
+        log.info("userExistInRoom chatroomId : {}  userId : {}", chatroomId, userId);
+        IMApiResultInfo resultInfo;
+        try {
+            resultInfo = imHelper.isInChartRoom(chatroomId, userId);
+        } catch (Exception e) {
+            throw new BizException("查询失败" + e.getMessage());
+        }
+        if (!resultInfo.isSuccess()) {
+            log.error("userExistInRoom  chatroomId : {}  userId : {}", chatroomId, userId);
+            throw new BizException("查询失败!");
+        }
+        return resultInfo.isSuccess() && resultInfo.getIsInChrm();
+    }
+
     public String getRoomSessionId(String roomId) {
         RBucket<String> bucket = redissonClient.getBucket("sessionId:" + roomId);
-        String sessionId = bucket.get();
-        if (StringUtils.isNotEmpty(sessionId)) {
-            return sessionId;
+        if (bucket.isExists()) {
+            return bucket.get();
         }
-        JSONObject jsonObject = new JSONObject();
-        jsonObject.put("roomId", roomId);
+        HttpURLConnection conn;
+        JSONObject resultObject;
 
-        HttpURLConnection conn = null;
         try {
+            JSONObject jsonObject = new JSONObject();
+            jsonObject.put("roomId", roomId);
             conn = httpHelper.createIMRtcPostHttpConnection("/rtc/room/query.json", "application/json", null);
             httpHelper.setBodyParameter(jsonObject.toJSONString(), conn);
             String returnResult = httpHelper.returnResult(conn, jsonObject.toJSONString());
-            JSONObject resultObject = JSONObject.parseObject(returnResult);
-            String code = resultObject.get("code").toString();
-            if ("200".equals(code)) {
-                sessionId = resultObject.get("sessionId").toString();
-                bucket.set(sessionId);
-            } else {
-                log.error("获取sessionId失败 returnResult:{}", returnResult);
-                throw new BizException("获取sessionId失败");
-            }
+            resultObject = JSONObject.parseObject(returnResult);
         } catch (Exception e) {
-            e.printStackTrace();
+            throw new BizException("获取sessionId失败", e.getCause());
         }
+
+        String sessionId;
+        if ("200".equals(resultObject.getString("code"))) {
+            sessionId = resultObject.getString("sessionId");
+            bucket.set(sessionId, 1, TimeUnit.HOURS);
+            log.info("getRoomSessionId roomId : {}  sessionId : {}", roomId, sessionId);
+        } else {
+            log.error("获取sessionId失败 roomId : {} returnResult:{}", roomId, resultObject);
+            throw new BizException("获取sessionId失败");
+        }
+
         return sessionId;
     }
 

+ 2 - 0
mec-im/src/main/java/com/ym/service/LiveRoomService.java

@@ -43,4 +43,6 @@ public interface LiveRoomService {
     */
     void recordSync(RecordNotify recordNotify);
 
+    boolean userExistInRoom(String chatroomId, String userId);
+
 }