Kaynağa Gözat

报错解决

yuhongqi 1 hafta önce
ebeveyn
işleme
887e66ac48

+ 1 - 0
fs-live-app/src/main/java/com/fs/live/websocket/constant/AttrConstant.java

@@ -18,4 +18,5 @@ public class AttrConstant {
     public static final AttributeKey<Long> ATTR_LIVE_ID = AttributeKey.valueOf(LIVE_ID);
     public static final AttributeKey<Long> ATTR_USER_ID = AttributeKey.valueOf(USER_ID);
     public static final AttributeKey<Long> ATTR_USER_TYPE = AttributeKey.valueOf(USER_TYPE);
+    public static final AttributeKey<String> ATTR_LOCATION = AttributeKey.valueOf(USER_TYPE);
 }

+ 2 - 1
fs-live-app/src/main/java/com/fs/live/websocket/handle/LiveChatHandler.java

@@ -58,6 +58,7 @@ public class LiveChatHandler extends SimpleChannelInboundHandler<TextWebSocketFr
             Long liveId = ctx.channel().attr(AttrConstant.ATTR_LIVE_ID).get();
             Long userType = ctx.channel().attr(AttrConstant.ATTR_USER_TYPE).get();
 
+
             if (Objects.isNull(liveService.selectLiveByLiveId(liveId))) {
                 ctx.channel().writeAndFlush(new TextWebSocketFrame("Error: 未找到直播间")).addListener(ChannelFutureListener.CLOSE);
                 return;
@@ -70,7 +71,7 @@ public class LiveChatHandler extends SimpleChannelInboundHandler<TextWebSocketFr
 
             if (userType == 0) {
                 // 加入房间
-                liveWatchUserService.join(liveId, userId);
+                liveWatchUserService.joinWithoutLocation(liveId, userId);
                 room.put(userId, ctx.channel());
 
                 FsUser fsUser = fsUserService.selectFsUserByUserId(userId);

+ 1 - 0
fs-service/src/main/java/com/fs/live/service/ILiveWatchUserService.java

@@ -76,6 +76,7 @@ public interface ILiveWatchUserService {
     LiveWatchUser getByLiveIdAndUserId(long liveId, long userId);
 
     LiveWatchUser join(long liveId, long userId, String location);
+    LiveWatchUser joinWithoutLocation(long liveId, long userId);
     LiveWatchUser close(long liveId, long userId);
 
     /**

+ 64 - 8
fs-service/src/main/java/com/fs/live/service/impl/LiveWatchUserServiceImpl.java

@@ -156,33 +156,33 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
         LiveWatchUser liveWatchUser = getByLiveIdAndUserId(liveId, userId);
         FsUser fsUserVO = fsUserService.selectFsUserByUserId(userId);
         Date now = DateUtils.getNowDate();
-        
+
         // 查询直播间信息
         Live live = liveMapper.selectLiveByLiveId(liveId);
-        
+
         // 判断用户进入时间:如果进入时间大于直播结束时间,说明是回放
         boolean isReplay = false;
         if (live != null && live.getFinishTime() != null) {
             Date finishTime = java.sql.Timestamp.valueOf(live.getFinishTime());
             isReplay = now.after(finishTime);
         }
-        
+
         if(liveWatchUser != null) {
             liveWatchUser.setUpdateTime(now);
             liveWatchUser.setOnline(0);
-            
+
             // 更新location
             if (StringUtils.isNotEmpty(location)) {
                 liveWatchUser.setLocation(location);
             }
-            
+
             // 更新进入标记
             if (isReplay) {
                 liveWatchUser.setReplayFlag(1);
             } else {
                 liveWatchUser.setLiveFlag(1);
             }
-            
+
             baseMapper.updateLiveWatchUser(liveWatchUser);
         }else{
             liveWatchUser = new LiveWatchUser();
@@ -192,7 +192,7 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
             liveWatchUser.setMsgStatus(0);
             liveWatchUser.setOnline(0);
             liveWatchUser.setLocation(location);
-            
+
             // 设置进入标记
             if (isReplay) {
                 liveWatchUser.setReplayFlag(1);
@@ -201,7 +201,63 @@ public class LiveWatchUserServiceImpl implements ILiveWatchUserService {
                 liveWatchUser.setLiveFlag(1);
                 liveWatchUser.setReplayFlag(0);
             }
-            
+
+            liveWatchUser.setCreateTime(now);
+            liveWatchUser.setUpdateTime(now);
+            baseMapper.insertLiveWatchUser(liveWatchUser);
+        }
+        liveWatchUser.setAvatar(fsUserVO.getAvatar());
+        liveWatchUser.setNickName(fsUserVO.getNickname());
+        String hashKey  = String.format(LiveKeysConstant.LIVE_WATCH_USERS, liveId);
+        redisCache.hashPut(hashKey, String.valueOf(userId), JSON.toJSONString(liveWatchUser));
+        return liveWatchUser;
+    }
+
+    @Override
+    public LiveWatchUser joinWithoutLocation(long liveId, long userId) {
+        LiveWatchUser liveWatchUser = getByLiveIdAndUserId(liveId, userId);
+        FsUser fsUserVO = fsUserService.selectFsUserByUserId(userId);
+        Date now = DateUtils.getNowDate();
+
+        // 查询直播间信息
+        Live live = liveMapper.selectLiveByLiveId(liveId);
+
+        // 判断用户进入时间:如果进入时间大于直播结束时间,说明是回放
+        boolean isReplay = false;
+        if (live != null && live.getFinishTime() != null) {
+            Date finishTime = java.sql.Timestamp.valueOf(live.getFinishTime());
+            isReplay = now.after(finishTime);
+        }
+
+        if(liveWatchUser != null) {
+            liveWatchUser.setUpdateTime(now);
+            liveWatchUser.setOnline(0);
+
+            // 更新进入标记
+            if (isReplay) {
+                liveWatchUser.setReplayFlag(1);
+            } else {
+                liveWatchUser.setLiveFlag(1);
+            }
+
+            baseMapper.updateLiveWatchUser(liveWatchUser);
+        }else{
+            liveWatchUser = new LiveWatchUser();
+            liveWatchUser.setLiveId(liveId);
+            liveWatchUser.setUserId(userId);
+            liveWatchUser.setAvatar(fsUserVO.getAvatar());
+            liveWatchUser.setMsgStatus(0);
+            liveWatchUser.setOnline(0);
+
+            // 设置进入标记
+            if (isReplay) {
+                liveWatchUser.setReplayFlag(1);
+                liveWatchUser.setLiveFlag(0);
+            } else {
+                liveWatchUser.setLiveFlag(1);
+                liveWatchUser.setReplayFlag(0);
+            }
+
             liveWatchUser.setCreateTime(now);
             liveWatchUser.setUpdateTime(now);
             baseMapper.insertLiveWatchUser(liveWatchUser);