|
@@ -517,61 +517,6 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 同步直播间在线人数 - 3秒一次发送到直播间消息中
|
|
|
|
- */
|
|
|
|
- @Override
|
|
|
|
- public void sendLiveOnlineNum() {
|
|
|
|
- //获取所有在直播中的直播间
|
|
|
|
- List<ImLiveBroadcastRoom> liveRooms = this.list(Wrappers.<ImLiveBroadcastRoom>lambdaQuery()
|
|
|
|
- .eq(ImLiveBroadcastRoom::getRoomState, 0)
|
|
|
|
- .eq(ImLiveBroadcastRoom::getLiveState, 1)
|
|
|
|
- );
|
|
|
|
- if (CollectionUtils.isEmpty(liveRooms)) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- log.info("sendLiveOnlineNum>>>> liveRooms{}", JSONObject.toJSONString(liveRooms));
|
|
|
|
- Map<String, Integer> onlineUserMap = new HashMap<>();
|
|
|
|
- liveRooms.forEach(room -> {
|
|
|
|
- //查询在线观看者信息
|
|
|
|
- RMap<Integer, BaseRoomUserVo> onlineUserCache = this.getOnlineUserCache(room.getRoomUid());
|
|
|
|
- if (!onlineUserCache.isExists()) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- //发送消息到直播间
|
|
|
|
- ImRoomMessage message = new ImRoomMessage();
|
|
|
|
- message.setFromUserId(room.getSpeakerId().toString());
|
|
|
|
- message.setToChatroomId(room.getRoomUid());
|
|
|
|
- message.setObjectName(ImRoomMessage.MEMBER_COUNT);
|
|
|
|
- int size = onlineUserCache.size();
|
|
|
|
- if (size < 1) {
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- onlineUserMap.put("count", size);
|
|
|
|
- message.setContent(onlineUserMap);
|
|
|
|
- //主讲人发送消息
|
|
|
|
- try {
|
|
|
|
- imFeignService.publishRoomMsg(message);
|
|
|
|
- log.info("sendLiveOnlineNum>>>> speakerId room:{}", JSONObject.toJSONString(message));
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- log.error("sendLiveOnlineNum>>>> speakerId error {}", e.getMessage());
|
|
|
|
- log.error("sendLiveOnlineNum>>>> speakerId sendMessage {} :", JSONObject.toJSONString(message));
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- //主讲人自己发消息自己收不到,需要一个观看者发送消息
|
|
|
|
- List<BaseRoomUserVo> collect = onlineUserCache.values().stream().limit(1).collect(Collectors.toList());
|
|
|
|
- BaseRoomUserVo baseRoomUserVo = collect.get(0);
|
|
|
|
- message.setFromUserId(baseRoomUserVo.getUserId().toString());
|
|
|
|
- try {
|
|
|
|
- imFeignService.publishRoomMsg(message);
|
|
|
|
- log.info("sendLiveOnlineNum>>>> looker room:{}", JSONObject.toJSONString(message));
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- log.error("sendLiveOnlineNum>>>> looker error {}", e.getMessage());
|
|
|
|
- log.error("sendLiveOnlineNum>>>> looker sendMessage {} :", JSONObject.toJSONString(message));
|
|
|
|
- }
|
|
|
|
- });
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
* <p>主讲人处理进入和退出房间数据
|
|
* <p>主讲人处理进入和退出房间数据
|
|
* <p>观看者只处理退出房间数据
|
|
* <p>观看者只处理退出房间数据
|
|
*
|
|
*
|
|
@@ -601,25 +546,11 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
|
|
}
|
|
}
|
|
//将最新的时间写入缓存
|
|
//将最新的时间写入缓存
|
|
userStateTimeCache.set(userStateTime, 5L, TimeUnit.MINUTES);
|
|
userStateTimeCache.set(userStateTime, 5L, TimeUnit.MINUTES);
|
|
- //查询主讲人userId,若是主讲人
|
|
|
|
- RBucket<RoomSpeakerInfo> speakerCache = redissonClient.getBucket(LIVE_SPEAKER_INFO.replace(USER_ID, userid));
|
|
|
|
- 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("opsRoom>>>> exit speakerCache {}", JSONObject.toJSONString(speakerInfo));
|
|
|
|
- speakerCache.set(speakerInfo);
|
|
|
|
|
|
+ //查询userId是不是主讲人 ,如果是主讲人则返回
|
|
|
|
+ if (isSpeaker(user, now, userid)) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- //未查询到主讲人信息,观看者只接受退出消息 status=0 是进入房间
|
|
|
|
|
|
+ //这里开始只处理观看者的数据,观看者只接受退出消息 status=0 是进入房间
|
|
if (user.getStatus().equals("0")) {
|
|
if (user.getStatus().equals("0")) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
@@ -637,50 +568,41 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
|
|
if (!roomTotalUser.isExists() && !roomTotalUser.containsKey(userId)) {
|
|
if (!roomTotalUser.isExists() && !roomTotalUser.containsKey(userId)) {
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- //查询到用户数据
|
|
|
|
|
|
+ //查询用户数据
|
|
RoomUserInfoVo userInfo = roomTotalUser.get(userId);
|
|
RoomUserInfoVo userInfo = roomTotalUser.get(userId);
|
|
- //查询在线人员列表
|
|
|
|
- RMap<Integer, BaseRoomUserVo> onlineUserInfo = getOnlineUserCache(roomUid);
|
|
|
|
- //获取当前用户是否在房间状态 true在 false不在
|
|
|
|
- boolean userOnline = onlineUserInfo.isExists() && onlineUserInfo.containsKey(userId);
|
|
|
|
- //用户是在房间的状态 并且 突然离线 - 那么融云会发送用户离线消息-此刻就发送退出房间消息给主讲人
|
|
|
|
- if (userOnline && user.getStatus().equals("1")) {
|
|
|
|
- ImRoomMessage message = new ImRoomMessage();
|
|
|
|
- message.setFromUserId(userId.toString());
|
|
|
|
- message.setToChatroomId(roomUid);
|
|
|
|
- message.setObjectName(ImRoomMessage.LOOKER_LOGIN_OUT);
|
|
|
|
- try {
|
|
|
|
- imFeignService.publishRoomMsg(message);
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- log.error("opsRoom>>>> looker error {}", e.getMessage());
|
|
|
|
- log.error("opsRoom>>>> looker error sendMessage {} : LOOKER_LOGIN_OUT : {}", message, JSONObject.toJSONString(userInfo));
|
|
|
|
- }
|
|
|
|
- log.info("opsRoom>>>> looker LOOKER_LOGIN_OUT : {}", JSONObject.toJSONString(userInfo));
|
|
|
|
- }
|
|
|
|
- //只有在主播开播后用户才有观看时间,才需要计算当前用户观看时长
|
|
|
|
|
|
+ //如果有动态观看时间则证明主播开播过,需要计算当前用户观看时长
|
|
if (Objects.nonNull(userInfo.getDynamicLookTime())) {
|
|
if (Objects.nonNull(userInfo.getDynamicLookTime())) {
|
|
userInfo.setTotalViewTime(getLookMinutes(userInfo.getDynamicLookTime(), userInfo.getTotalViewTime()));
|
|
userInfo.setTotalViewTime(getLookMinutes(userInfo.getDynamicLookTime(), userInfo.getTotalViewTime()));
|
|
userInfo.setDynamicLookTime(null);
|
|
userInfo.setDynamicLookTime(null);
|
|
}
|
|
}
|
|
roomTotalUser.fastPut(userId, userInfo);
|
|
roomTotalUser.fastPut(userId, userInfo);
|
|
- //如果是最后一个人离开房间则发送一条消息给主播
|
|
|
|
- if (onlineUserInfo.isExists() && onlineUserInfo.size() == 1) {
|
|
|
|
- //发送消息到直播间
|
|
|
|
- ImRoomMessage message = new ImRoomMessage();
|
|
|
|
- message.setFromUserId(userId.toString());
|
|
|
|
- message.setToChatroomId(roomUid);
|
|
|
|
- message.setObjectName(ImRoomMessage.MEMBER_COUNT);
|
|
|
|
- message.setContent(new HashMap<String, Integer>() {{
|
|
|
|
- put("count", 0);
|
|
|
|
- }});
|
|
|
|
- //发送消息
|
|
|
|
- try {
|
|
|
|
- imFeignService.publishRoomMsg(message);
|
|
|
|
- log.info("opsRoom>>>> sendLiveOnlineNum>>>> speakerId room:{}", JSONObject.toJSONString(message));
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- log.error("opsRoom>>>> sendLiveOnlineNum>>>> speakerId error {}", e.getMessage());
|
|
|
|
- log.error("opsRoom>>>> sendLiveOnlineNum>>>> speakerId sendMessage {} :", JSONObject.toJSONString(message));
|
|
|
|
- }
|
|
|
|
|
|
+
|
|
|
|
+ //查询在线人员列表
|
|
|
|
+ RMap<Integer, BaseRoomUserVo> onlineUserInfo = getOnlineUserCache(roomUid);
|
|
|
|
+ if (!onlineUserInfo.isExists()) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ //发消息给主播 告知现在人数
|
|
|
|
+ int size = 0;
|
|
|
|
+ ImRoomMessage message = new ImRoomMessage();
|
|
|
|
+ message.setIsIncludeSender(1);
|
|
|
|
+ message.setObjectName(ImRoomMessage.MEMBER_COUNT);
|
|
|
|
+ message.setToChatroomId(roomUid);
|
|
|
|
+ //大于1就发送实际人数,如果是最后一个人离开房间则发送一条0人数消息给主播
|
|
|
|
+ if (onlineUserInfo.size() > 1) {
|
|
|
|
+ size = onlineUserInfo.size();
|
|
|
|
+ }
|
|
|
|
+ HashMap<String, Integer> sendMap = new HashMap<>();
|
|
|
|
+ sendMap.put("count", size);
|
|
|
|
+ message.setFromUserId(userId.toString());
|
|
|
|
+ message.setContent(sendMap);
|
|
|
|
+ //发送消息
|
|
|
|
+ try {
|
|
|
|
+ imFeignService.publishRoomMsg(message);
|
|
|
|
+ log.info("opsRoom>>>> sendLiveOnlineNum>>>> speakerId room:{}", JSONObject.toJSONString(message));
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error("opsRoom>>>> sendLiveOnlineNum>>>> speakerId error {}", e.getMessage());
|
|
|
|
+ log.error("opsRoom>>>> sendLiveOnlineNum>>>> speakerId sendMessage {} :", JSONObject.toJSONString(message));
|
|
}
|
|
}
|
|
//从在线人员列表删除该人员
|
|
//从在线人员列表删除该人员
|
|
onlineUserInfo.fastRemove(userId);
|
|
onlineUserInfo.fastRemove(userId);
|
|
@@ -689,6 +611,30 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
|
|
+ * 查询userId是不是主讲人
|
|
|
|
+ */
|
|
|
|
+ private boolean isSpeaker(ImUserState user, Date now, String userid) {
|
|
|
|
+ RBucket<RoomSpeakerInfo> speakerCache = redissonClient.getBucket(LIVE_SPEAKER_INFO.replace(USER_ID, userid));
|
|
|
|
+ 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 true;
|
|
|
|
+ }
|
|
|
|
+ //主讲人退出房间关闭录像
|
|
|
|
+ closeLive(speakerInfo);
|
|
|
|
+ speakerInfo.setExitRoomTime(now);
|
|
|
|
+ log.info("opsRoom>>>> exit speakerCache {}", JSONObject.toJSONString(speakerInfo));
|
|
|
|
+ speakerCache.set(speakerInfo);
|
|
|
|
+ return true;
|
|
|
|
+ }
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
* 主讲人登录专用
|
|
* 主讲人登录专用
|
|
*
|
|
*
|
|
* @param roomUid
|
|
* @param roomUid
|