Browse Source

修改:监听融云用户状态逻辑

hgw 3 years ago
parent
commit
5fdeeb473e

+ 1 - 1
mec-biz/src/main/java/com/ym/mec/biz/service/ImLiveBroadcastRoomService.java

@@ -39,7 +39,7 @@ public interface ImLiveBroadcastRoomService extends IService<ImLiveBroadcastRoom
 
     void syncLike(String roomUid, Integer likeNum);
 
-    void quitRoom(List<ImUserState> userState);
+    void opsRoom(List<ImUserState> userState);
 
     ImLiveBroadcastRoomVo speakerJoinRoom(String roomUid);
 

+ 25 - 14
mec-biz/src/main/java/com/ym/mec/biz/service/impl/ImLiveBroadcastRoomServiceImpl.java

@@ -276,8 +276,8 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
 
                 //1.主播没有进入房间,则直接销毁房间
                 if (Objects.isNull(speakerInfo.getJoinRoomTime())) {
+                    log.info("roomDestroy not joinRoom >>>> cache : {}", JSONObject.toJSONString(test(room.getRoomUid(), room.getSpeakerId())));
                     roomDestroy(room);
-                    log.info("roomDestroy not joinRoom >>>> roomId:{}", room.getId());
                     return;
                 }
 
@@ -288,8 +288,8 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
                         //如果退出时间大于进入时间,就将退出时间+expiredMinute分钟
                         Date exitExpiredTime = DateUtil.addMinutes(speakerInfo.getExitRoomTime(), expiredMinute);
                         if (now.getTime() >= exitExpiredTime.getTime()) {
+                            log.info("roomDestroy exitExpiredTime >>>> cache : {}", JSONObject.toJSONString(test(room.getRoomUid(), room.getSpeakerId())));
                             roomDestroy(room);
-                            log.info("roomDestroy exitExpiredTime >>>> roomId:{}", room.getId());
                         }
                     }
                 }
@@ -425,40 +425,51 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
     }
 
     /**
-     * 退出房间-修改用户对应的房间号信息
+     * <p>主讲人处理进入和退出房间数据
+     * <p>观看者只处理退出房间数据
      *
      * @param userState 用户状态数据
      */
     @Override
-    public void quitRoom(List<ImUserState> userState) {
+    public void opsRoom(List<ImUserState> userState) {
         if (CollectionUtils.isEmpty(userState)) {
             return;
         }
         userState.forEach(user -> {
-            log.info("quitRoom>>>> {}", JSONObject.toJSONString(user));
-            if (StringUtils.isBlank(user.getStatus()) || user.getStatus().equals("0")) {
+            log.info("opsRoom>>>> {}", JSONObject.toJSONString(user));
+            if (StringUtils.isBlank(user.getStatus())) {
                 return;
             }
-            Integer userId = Integer.valueOf(user.getUserid());
             Date now = new Date();
-            //查询userId,如果是主讲人退出房间,那么不做后面操作
+            //查询主讲人userId,若是主讲人
             RBucket<RoomSpeakerInfo> speakerCache = redissonClient.getBucket(LIVE_SPEAKER_INFO.replace(USER_ID, user.getUserid()));
             if (speakerCache.isExists()) {
                 RoomSpeakerInfo speakerInfo = speakerCache.get();
+                //主讲人进入房间
+                if (user.getStatus().equals("0")) {
+                    speakerInfo.setJoinRoomTime(now);
+                    log.info("opsRoom>>>> join speakerCache {}", JSONObject.toJSONString(speakerInfo));
+                    speakerCache.set(speakerInfo);
+                    return;
+                }
                 //主讲人退出房间关闭录像
                 closeLive(speakerInfo);
                 speakerInfo.setExitRoomTime(now);
-                log.info("quitRoom>>>> speakerCache {}", JSONObject.toJSONString(speakerInfo));
+                log.info("opsRoom>>>> exit speakerCache {}", JSONObject.toJSONString(speakerInfo));
                 speakerCache.set(speakerInfo);
                 return;
             }
-
+            //未查询到主讲人信息,观看者只接受退出消息 status=0 是进入房间
+            if (user.getStatus().equals("0")) {
+                return;
+            }
             //获取当前用户所在房间的uid
             RBucket<String> userRoom = redissonClient.getBucket(LIVE_USER_ROOM.replace(USER_ID, user.getUserid()));
             if (!userRoom.isExists()) {
                 return;
             }
             String roomUid = userRoom.get();
+            Integer userId = Integer.valueOf(user.getUserid());
 
             //从房间累计用户信息中查询该用户的信息
             RMap<Integer, RoomUserInfoVo> roomTotalUser = redissonClient.getMap(LIVE_ROOM_TOTAL_USER_LIST.replace(ROOM_UID, roomUid));
@@ -477,10 +488,10 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
                 try {
                     imFeignService.publishRoomMsg(message);
                 } catch (Exception e) {
-                    log.error("quitRoom>>>>  error {}", e.getMessage());
-                    log.error("quitRoom>>>>  error sendMessage {} : LOOKER_LOGIN_OUT : {}", message, JSONObject.toJSONString(userInfo));
+                    log.error("opsRoom>>>>  looker error {}", e.getMessage());
+                    log.error("opsRoom>>>>  looker error sendMessage {} : LOOKER_LOGIN_OUT : {}", message, JSONObject.toJSONString(userInfo));
                 }
-                log.info("quitRoom>>>> LOOKER_LOGIN_OUT : {}", JSONObject.toJSONString(userInfo));
+                log.info("opsRoom>>>> looker LOOKER_LOGIN_OUT : {}", JSONObject.toJSONString(userInfo));
             }
             //每次退出房间计算当前用户观看时长
             int minutesBetween = getMinutesBetween(userInfo.getDynamicJoinTime(), now);
@@ -489,7 +500,7 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
             userInfo.setLastOutTime(now);
             userInfo.setState(1);
             roomTotalUser.fastPut(userId, userInfo);
-            log.info("quitRoom>>>> userInfo: {}", JSONObject.toJSONString(userInfo));
+            log.info("opsRoom>>>> looker userInfo: {}", JSONObject.toJSONString(userInfo));
         });
 
     }

+ 1 - 8
mec-im/src/main/java/com/ym/controller/UserController.java

@@ -6,14 +6,12 @@ import com.ym.mec.biz.service.ImLiveBroadcastRoomService;
 import com.ym.mec.common.entity.ImUserState;
 import com.ym.service.UserService;
 import io.rong.models.user.UserModel;
-import org.apache.commons.lang3.StringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.web.bind.annotation.*;
 
 import java.util.List;
-import java.util.stream.Collectors;
 
 @RestController
 @RequestMapping("/user")
@@ -42,12 +40,7 @@ public class UserController {
     @PostMapping(value = "/statusImUser")
     public BaseResponse statusImUser(@RequestBody List<ImUserState> userState) {
         log.info("statusImUser >>>>> : {}", JSONObject.toJSONString(userState));
-        //目前只监听用户不在线 退出 断开连接类型的状态
-        List<ImUserState> quitCollect = userState.stream()
-                .filter(user -> !"0".equals(user.getStatus()))
-                .collect(Collectors.toList());
-        log.info("statusImUser >>>>> quitCollect : {}", JSONObject.toJSONString(quitCollect));
-        imLiveBroadcastRoomService.quitRoom(quitCollect);
+        imLiveBroadcastRoomService.opsRoom(userState);
         return new BaseResponse<>();
     }
 }

+ 1 - 1
mec-web/src/main/java/com/ym/mec/web/controller/ImLiveBroadcastRoomController.java

@@ -125,7 +125,7 @@ public class ImLiveBroadcastRoomController extends BaseController {
 
     @PostMapping("/quitRoom")
     public HttpResponseResult quitRoom(@RequestBody List<ImUserState> userState) {
-        imLiveBroadcastRoomService.quitRoom(userState);
+        imLiveBroadcastRoomService.opsRoom(userState);
         return succeed();
     }