|
@@ -26,7 +26,6 @@ import com.ym.mec.util.date.DateUtil;
|
|
|
import com.ym.mec.util.http.HttpUtil;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.redisson.api.RBucket;
|
|
|
-import org.redisson.api.RList;
|
|
|
import org.redisson.api.RMap;
|
|
|
import org.redisson.api.RedissonClient;
|
|
|
import org.slf4j.Logger;
|
|
@@ -67,8 +66,6 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
|
|
|
public static final String LIVE_ROOM_TOTAL_USER_LIST = "IM:LIVE_ROOM_TOTAL_USER_LIST:" + ROOM_UID;
|
|
|
//用户对应的直播间Uid
|
|
|
public static final String LIVE_USER_ROOM = "IM:LIVE_ROOM_USER:" + USER_ID;
|
|
|
- //房间主讲人id
|
|
|
- public static final String LIVE_ROOM_SPEAKERID = "IM:LIVE_ROOM_SPEAKERID:" + ROOM_UID;
|
|
|
//房间点赞数
|
|
|
public static final String LIVE_ROOM_LIKE = "IM:LIVE_ROOM_LIKE:" + ROOM_UID;
|
|
|
|
|
@@ -245,6 +242,9 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
|
|
|
*/
|
|
|
public void quitRoom(List<ImUserState> userState) {
|
|
|
userState.forEach(user -> {
|
|
|
+ if (StringUtils.isBlank(user.getStatus()) || user.getStatus().equals("0")) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
//获取当前用户所在房间的uid
|
|
|
RBucket<String> userRoom = redissonClient.getBucket(LIVE_USER_ROOM.replace(USER_ID, user.getUserid()));
|
|
|
if (!userRoom.isExists()) {
|
|
@@ -253,31 +253,27 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
|
|
|
String roomUid = userRoom.get();
|
|
|
Integer userId = Integer.valueOf(user.getUserid());
|
|
|
|
|
|
- //发送退出房间消息给主讲人
|
|
|
- ImRoomMessage message = new ImRoomMessage();
|
|
|
- message.setFromUserId(userId.toString());
|
|
|
- message.setToChatroomId(roomUid);
|
|
|
- message.setObjectName(ImRoomMessage.LOOKER_LOGIN_OUT);
|
|
|
- imFeignService.publishRoomMsg(message);
|
|
|
-
|
|
|
- //将用户从该房间当前用户列表移除
|
|
|
- removeUserCache(userId, roomUid);
|
|
|
+ //只有离线-发送退出房间消息给主讲人
|
|
|
+ if (user.getStatus().equals("1")) {
|
|
|
+ ImRoomMessage message = new ImRoomMessage();
|
|
|
+ message.setFromUserId(userId.toString());
|
|
|
+ message.setToChatroomId(roomUid);
|
|
|
+ message.setObjectName(ImRoomMessage.LOOKER_LOGIN_OUT);
|
|
|
+ imFeignService.publishRoomMsg(message);
|
|
|
+ }
|
|
|
|
|
|
//从房间累计用户信息中查询该用户的信息
|
|
|
RMap<Integer, RoomUserInfoVo> roomTotalUser = redissonClient.getMap(LIVE_ROOM_TOTAL_USER_LIST.replace(ROOM_UID, roomUid));
|
|
|
Date now = new Date();
|
|
|
RoomUserInfoVo userInfo;
|
|
|
- //防止数据问题导致写漏了进入房间的数据,这里还是要判断一下是否有用户数据
|
|
|
- if (roomTotalUser.containsKey(userId)) {
|
|
|
- //查询到用户数据
|
|
|
- userInfo = roomTotalUser.get(userId);
|
|
|
- } else {
|
|
|
- //未查询到用户数据则新建立用户数据 如果没有观看时长则取开播时间
|
|
|
- userInfo = getUserInfo(userId);
|
|
|
- ImLiveBroadcastRoomVo imLiveBroadcastRoomVo = queryRoomInfo(roomUid);
|
|
|
- userInfo.setFirstJoinTime(imLiveBroadcastRoomVo.getLiveStartTime());
|
|
|
- userInfo.setDynamicJoinTime(imLiveBroadcastRoomVo.getLiveStartTime());
|
|
|
+ //该房间未查询到用户数据则不处理
|
|
|
+ if (!roomTotalUser.containsKey(userId)) {
|
|
|
+ return;
|
|
|
}
|
|
|
+ //查询到用户数据
|
|
|
+ userInfo = roomTotalUser.get(userId);
|
|
|
+ //将用户从该房间当前直播间用户列表移除
|
|
|
+ redissonClient.getList(LIVE_ROOM_USER_LIST.replace(ROOM_UID, roomUid)).remove(userInfo);
|
|
|
//每次退出房间计算当前用户观看时长
|
|
|
int minutesBetween = getMinutesBetween(userInfo.getDynamicJoinTime(), now);
|
|
|
userInfo.setTotalViewTime(userInfo.getTotalViewTime() + minutesBetween);
|
|
@@ -285,6 +281,7 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
|
|
|
userInfo.setLastOutTime(now);
|
|
|
roomTotalUser.put(userId, userInfo);
|
|
|
});
|
|
|
+
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -294,22 +291,8 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
|
|
|
* @param userId 用户id
|
|
|
*/
|
|
|
public void joinRoom(String roomUid, Integer userId) {
|
|
|
- //查询用户当前对应的直播间信息
|
|
|
- RBucket<String> roomUserCache = redissonClient.getBucket(LIVE_USER_ROOM.replace(USER_ID, userId.toString()));
|
|
|
- //获取当前房间信息,如果当前有房间
|
|
|
- if (roomUserCache.isExists()) {
|
|
|
- //查出原有的房间uid
|
|
|
- String lodRoomUid = roomUserCache.get();
|
|
|
- //如果房间相同直接返回
|
|
|
- if (roomUid.equals(lodRoomUid)) {
|
|
|
- return;
|
|
|
- } else {
|
|
|
- //如果不同就把原房间的当前人员移除
|
|
|
- removeUserCache(userId, lodRoomUid);
|
|
|
- }
|
|
|
- }
|
|
|
//记录用户当前房间uid
|
|
|
- roomUserCache.set(roomUid);
|
|
|
+ redissonClient.getBucket(LIVE_USER_ROOM.replace(USER_ID, userId.toString())).set(roomUid);
|
|
|
//房间累计用户信息-指只要进入到该房间的用户都要记录
|
|
|
RMap<Integer, RoomUserInfoVo> roomTotalUser = redissonClient.getMap(LIVE_ROOM_TOTAL_USER_LIST.replace(ROOM_UID, roomUid));
|
|
|
//判断是否第一次进房间
|
|
@@ -372,10 +355,7 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
|
|
|
|
|
|
//移除
|
|
|
private void removeUserCache(Integer userId, String lodRoomUid) {
|
|
|
- //将用户数据从直播间用户列表删除
|
|
|
- RList<RoomUserInfoVo> list = redissonClient.getList(LIVE_ROOM_USER_LIST.replace(ROOM_UID, lodRoomUid));
|
|
|
- RoomUserInfoVo userInfo = getUserInfo(userId);
|
|
|
- list.remove(userInfo);
|
|
|
+
|
|
|
}
|
|
|
|
|
|
private RoomUserInfoVo getUserInfo(Integer userId) {
|
|
@@ -417,10 +397,11 @@ public class ImLiveBroadcastRoomServiceImpl extends ServiceImpl<ImLiveBroadcastR
|
|
|
}
|
|
|
//生成0点赞
|
|
|
redissonClient.getAtomicLong(LIVE_ROOM_LIKE.replace(ROOM_UID, room.getRoomUid())).set(0);
|
|
|
- //主讲人id
|
|
|
- redissonClient.getBucket(LIVE_ROOM_SPEAKERID.replace(ROOM_UID, room.getRoomUid())).set(room.getSpeakerId());
|
|
|
//todo 修改房间状态
|
|
|
-
|
|
|
+ room.setLiveState(1);
|
|
|
+ room.setUpdatedTime(new Date());
|
|
|
+ room.setUpdatedBy(-1);
|
|
|
+ baseMapper.updateById(room);
|
|
|
}
|
|
|
|
|
|
private void getRoomData(ImLiveBroadcastRoomVo roomVo) {
|