Forráskód Böngészése

增加 直播间人员变动监控方法

hgw 3 éve
szülő
commit
f5a72f1736

+ 2 - 0
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/constant/LiveRoomConstant.java

@@ -23,4 +23,6 @@ public interface LiveRoomConstant {
     //房间的信息
     String LIVE_ROOM_INFO = String.join(":", COOLESHOW, "LIVE_ROOM_INFO", ROOM_UID);
 
+    //记录老师最后变更的状态消息时间
+    String LIVE_TEACHER_LAST_TIME = String.join(":", COOLESHOW, "LIVE_TEACHER_LAST_TIME", USER_ID);
 }

+ 9 - 4
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/service/LiveRoomService.java

@@ -2,10 +2,7 @@ package com.yonge.cooleshow.biz.dal.service;
 
 import com.baomidou.mybatisplus.extension.service.IService;
 import com.yonge.cooleshow.biz.dal.dao.LiveRoomDao;
-import com.yonge.cooleshow.biz.dal.entity.ImRoomMessage;
-import com.yonge.cooleshow.biz.dal.entity.LiveRoom;
-import com.yonge.cooleshow.biz.dal.entity.RoomInfoCache;
-import com.yonge.cooleshow.biz.dal.entity.RoomUserInfoCache;
+import com.yonge.cooleshow.biz.dal.entity.*;
 import com.yonge.cooleshow.biz.dal.vo.RoomVo;
 
 import java.util.List;
@@ -47,6 +44,14 @@ public interface LiveRoomService extends IService<LiveRoom> {
     String createTempLiveRoom(Map<String, Object> param);
 
     /**
+     * <p>主讲人处理进入和退出房间数据
+     * <p>观看者只处理退出房间数据
+     *
+     * @param userState 用户状态数据
+     */
+    void opsRoom(List<ImUserStateSync> userState);
+
+    /**
      * 同步点赞数量
      *
      * @param roomUid 房间uid

+ 98 - 2
cooleshow-user/user-biz/src/main/java/com/yonge/cooleshow/biz/dal/service/impl/LiveRoomServiceImpl.java

@@ -325,6 +325,102 @@ public class LiveRoomServiceImpl extends ServiceImpl<LiveRoomDao, LiveRoom> impl
         log.info("destroyLiveRoom success: {}", roomId);
     }
 
+
+    /**
+     * <p>主讲人处理进入和退出房间数据
+     * <p>观看者只处理退出房间数据
+     *
+     * @param userState 用户状态数据
+     */
+    public void opsRoom(List<ImUserStateSync> userState) {
+        if (CollectionUtils.isEmpty(userState)) {
+            return;
+        }
+        userState.forEach(user -> {
+            log.info("opsRoom>>>> {}", JSONObject.toJSONString(user));
+            if (StringUtils.isBlank(user.getStatus())) {
+                return;
+            }
+            Date now = new Date();
+            //获取当前用户状态变更的时间
+            long userStateTime = Optional.ofNullable(user.getTime()).orElse(now.getTime());
+            String userIdStr = user.getUserid();
+            RBucket<Long> userStateTimeCache = redissonClient.getBucket(LIVE_TEACHER_LAST_TIME.replace(USER_ID, userIdStr));
+            if (userStateTimeCache.isExists()) {
+                //缓存的时间比当前传入时间大则放弃这条数据
+                long cacheTime = userStateTimeCache.get();
+                if (cacheTime > userStateTime) {
+                    return;
+                }
+            }
+            //将最新的时间写入缓存
+            userStateTimeCache.set(userStateTime, 5L, TimeUnit.MINUTES);
+            //直播间号
+            String roomUid;
+            //根据用户id获取用户当前房间号
+            RBucket<String> roomUidCache = redissonClient.getBucket(LIVE_USER_ROOM.replace(USER_ID, userIdStr));
+            if (!roomUidCache.isExists()) {
+                return;
+            }
+            roomUid = roomUidCache.get();
+            //根据房间号获取房间信息
+            RBucket<RoomInfoCache> roomInfoCache = redissonClient.getBucket(LIVE_ROOM_INFO.replace(ROOM_UID, roomUid));
+            if (!roomInfoCache.isExists()) {
+                return;
+            }
+            RoomInfoCache roomInfo = roomInfoCache.get();
+            //主讲人
+            if (roomInfo.getSpeakerId().toString().equals(userIdStr)) {
+                //主讲人进入房间
+                if (user.getStatus().equals("0")) {
+                    roomInfo.setSpeakerState(0);
+                    roomInfo.setJoinRoomTime(now);
+                    log.info("opsRoom>>>> join roomInfo {}", JSONObject.toJSONString(roomInfo));
+                    roomInfoCache.set(roomInfo);
+                    return;
+                }
+                roomInfo.setExitRoomTime(now);
+                log.info("opsRoom>>>> exit roomInfo {}", JSONObject.toJSONString(roomInfo));
+                roomInfoCache.set(roomInfo);
+                return;
+            }
+            //观看者只接受退出消息 status=0 是进入房间
+            if (user.getStatus().equals("0")) {
+                return;
+            }
+            //观看者
+            Long userId = Long.valueOf(userIdStr);
+            //从房间累计用户信息中查询该用户的信息
+            RMap<Long, RoomUserInfoCache> roomTotalUser = redissonClient.getMap(LIVE_ROOM_TOTAL_USER_LIST.replace(ROOM_UID, roomUid));
+            //该房间未查询到用户数据则不处理
+            if (!roomTotalUser.isExists() && !roomTotalUser.containsKey(userId)) {
+                return;
+            }
+            //查询到用户数据
+            RoomUserInfoCache userInfo = roomTotalUser.get(userId);
+            //用户是在房间的状态 并且 突然离线 - 那么融云会发送用户离线消息-此刻就发送退出房间消息给主讲人
+            if (userInfo.getState() == 0 && user.getStatus().equals("1")) {
+                ImRoomMessage message = new ImRoomMessage();
+                message.setFromUserId(userId.toString());
+                message.setToChatroomId(roomUid);
+                message.setObjectName(ImRoomMessage.RC_CHATROOM_LEAVE);
+                try {
+                    publishRoomMessage(message);
+                } catch (Exception e) {
+                    log.error("opsRoom>>>>  looker error {}", e.getMessage());
+                    log.error("opsRoom>>>>  looker error sendMessage {} : leave : {}", message, JSONObject.toJSONString(userInfo));
+                }
+                log.info("opsRoom>>>> looker leave : {}", JSONObject.toJSONString(userInfo));
+            }
+            //记录退出时间 并写入缓存
+            userInfo.setLastOutTime(now);
+            userInfo.setState(1);
+            roomTotalUser.fastPut(userId, userInfo);
+            log.info("opsRoom>>>> looker userInfo: {}", JSONObject.toJSONString(userInfo));
+        });
+
+    }
+
     /**
      * 同步点赞数量
      *
@@ -352,7 +448,7 @@ public class LiveRoomServiceImpl extends ServiceImpl<LiveRoomDao, LiveRoom> impl
         roomInfo.setLikeNum(getLike(roomUid));
         roomInfo.setLookNum(getLooker(roomUid));
 
-        //记录当前对应的房间uid
+        //记录当前用户对应的房间uid
         redissonClient.getBucket(LIVE_USER_ROOM.replace(USER_ID, userId.toString())).set(roomUid, 2L, TimeUnit.DAYS);
 
         Date now = new Date();
@@ -484,7 +580,7 @@ public class LiveRoomServiceImpl extends ServiceImpl<LiveRoomDao, LiveRoom> impl
         param.put("year", "2022");
         param.put("singleCourseMinutes", 60);
         param.put("teacherId", 4);
-        List<CourseCalendarEntity> courseTimeEntities =  courseScheduleService.createLiveCourseCalendar(param);
+        List<CourseCalendarEntity> courseTimeEntities = courseScheduleService.createLiveCourseCalendar(param);
         result.put("自动生成课时", courseTimeEntities);
 
         //获取房间信息

+ 101 - 0
cooleshow-user/user-student/src/main/java/com/yonge/cooleshow/student/controller/StudentLiveRoomController.java

@@ -0,0 +1,101 @@
+package com.yonge.cooleshow.student.controller;
+
+
+import com.alibaba.fastjson.JSONObject;
+import com.yonge.cooleshow.biz.dal.entity.ImUserStateSync;
+import com.yonge.cooleshow.biz.dal.entity.RoomInfoCache;
+import com.yonge.cooleshow.biz.dal.service.LiveRoomService;
+import com.yonge.cooleshow.biz.dal.vo.RoomVo;
+import com.yonge.cooleshow.common.controller.BaseController;
+import com.yonge.cooleshow.common.entity.HttpResponseResult;
+import io.swagger.annotations.*;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.web.bind.annotation.*;
+
+import javax.annotation.Resource;
+import java.util.List;
+import java.util.Map;
+
+/**
+ * 直播房间与课程的关系表表(LiveRoom)表控制层
+ *
+ * @author hgw
+ * @since 2022-03-18 15:41:16
+ */
+@Api(tags = "直播房间与课程的关系表表")
+@RestController
+@RequestMapping("/liveRoom")
+public class StudentLiveRoomController extends BaseController {
+    private final static Logger log = LoggerFactory.getLogger(StudentLiveRoomController.class);
+    /**
+     * 服务对象
+     */
+    @Resource
+    private LiveRoomService liveRoomService;
+
+    /**
+     * 根据房间uid查询房间信息
+     *
+     * @param roomUid 房间uid
+     */
+    @GetMapping("queryRoomInfo")
+    public HttpResponseResult<RoomVo> queryRoomInfo(@RequestParam("roomUid") String roomUid) {
+        return succeed(liveRoomService.queryRoomInfo(roomUid));
+    }
+
+    @ApiImplicitParams({
+            @ApiImplicitParam(name = "roomTitle", dataType = "String", value = "房间标题"),
+            @ApiImplicitParam(name = "liveRemark", dataType = "String", value = "直播内容/最多200个字"),
+    })
+    @ApiOperation("创建临时房间-直播间")
+    @PostMapping("/createTempLiveRoom")
+    public HttpResponseResult<String> createTempLiveRoom(@RequestBody Map<String, Object> param) {
+        return succeed(liveRoomService.createTempLiveRoom(param));
+    }
+
+    @ApiOperation("同步点赞数量")
+    @GetMapping("/syncLike")
+    public HttpResponseResult<Object> syncLike(@ApiParam(value = "房间uid", required = true) String roomUid,
+                                       @ApiParam(value = "点赞数", required = true) Integer likeNum) {
+        liveRoomService.syncLike(roomUid, likeNum);
+        return succeed();
+    }
+
+    @ApiOperation("进入房间")
+    @GetMapping(value = "/joinRoom")
+    public HttpResponseResult<RoomInfoCache> joinRoom(String roomUid, Long userId) {
+        return succeed(liveRoomService.joinRoom(roomUid, userId));
+    }
+
+    @ApiOperation("定时任务-创建房间-直播间")
+    @GetMapping("/createCourseLiveRoom")
+    public void createCourseLiveRoom() {
+        liveRoomService.createCourseLiveRoom();
+    }
+
+    @ApiOperation("定时任务-销毁房间-直播间")
+    @GetMapping("/destroyExpiredLiveRoom")
+    public void destroyExpiredLiveRoom() {
+        liveRoomService.destroyExpiredLiveRoom();
+    }
+
+    /**
+     * 同步融云用户状态变更
+     *
+     * @param userState
+     */
+    @PostMapping(value = "/syncUserStatus")
+    public void statusImUser(@RequestBody List<ImUserStateSync> userState) {
+        log.info("opsRoom >>>>> : {}", JSONObject.toJSONString(userState));
+        liveRoomService.opsRoom(userState);
+    }
+
+    @ApiOperation("方便测试观察房间数据的方法")
+    @GetMapping("/test")
+    public Object destroyExpiredLiveRoom(@RequestParam("roomUid") String roomUid) {
+        return liveRoomService.test(roomUid);
+    }
+
+}
+

+ 4 - 3
cooleshow-user/user-teacher/src/main/java/com/yonge/cooleshow/teacher/controller/LiveRoomController.java → cooleshow-user/user-teacher/src/main/java/com/yonge/cooleshow/teacher/controller/TeacherLiveRoomController.java

@@ -26,8 +26,8 @@ import java.util.Map;
 @Api(tags = "直播房间与课程的关系表表")
 @RestController
 @RequestMapping("/liveRoom")
-public class LiveRoomController extends BaseController {
-    private final static Logger log = LoggerFactory.getLogger(LiveRoomController.class);
+public class TeacherLiveRoomController extends BaseController {
+    private final static Logger log = LoggerFactory.getLogger(TeacherLiveRoomController.class);
     /**
      * 服务对象
      */
@@ -87,7 +87,8 @@ public class LiveRoomController extends BaseController {
      */
     @PostMapping(value = "/syncUserStatus")
     public void statusImUser(@RequestBody List<ImUserStateSync> userState) {
-        log.info("statusImUser >>>>> : {}", JSONObject.toJSONString(userState));
+        log.info("opsRoom >>>>> : {}", JSONObject.toJSONString(userState));
+        liveRoomService.opsRoom(userState);
     }
 
     @ApiOperation("方便测试观察房间数据的方法")