瀏覽代碼

直播最大人数在线统计修改 直播定时任务修改权限

yuhongqi 3 周之前
父節點
當前提交
4e909a70dd

+ 6 - 6
fs-admin/src/main/java/com/fs/live/controller/LiveAutoTaskController.java

@@ -46,7 +46,7 @@ public class LiveAutoTaskController extends BaseController
     /**
      * 查询直播间自动化任务配置列表
      */
-//    @PreAuthorize("@ss.hasPermi('shop:task:list')")
+    @PreAuthorize("@ss.hasPermi('live:task:list')")
     @GetMapping("/list")
     public TableDataInfo list(LiveAutoTask liveAutoTask)
     {
@@ -69,7 +69,7 @@ public class LiveAutoTaskController extends BaseController
     /**
      * 导出直播间自动化任务配置列表
      */
-//    @PreAuthorize("@ss.hasPermi('shop:task:export')")
+    @PreAuthorize("@ss.hasPermi('live:task:export')")
     @Log(title = "直播间自动化任务配置", businessType = BusinessType.EXPORT)
     @GetMapping("/export")
     public AjaxResult export(LiveAutoTask liveAutoTask)
@@ -82,7 +82,7 @@ public class LiveAutoTaskController extends BaseController
     /**
      * 获取直播间自动化任务配置详细信息
      */
-//    @PreAuthorize("@ss.hasPermi('shop:task:query')")
+    @PreAuthorize("@ss.hasPermi('live:task:list')")
     @GetMapping(value = "/{id}")
     public AjaxResult getInfo(@PathVariable("id") Long id)
     {
@@ -92,7 +92,7 @@ public class LiveAutoTaskController extends BaseController
     /**
      * 新增直播间自动化任务配置
      */
-//    @PreAuthorize("@ss.hasPermi('shop:task:add')")
+//    @PreAuthorize("@ss.hasPermi('live:task:add')")
     @Log(title = "直播间自动化任务配置", businessType = BusinessType.INSERT)
     @PostMapping
     public R add(@RequestBody LiveAutoTask liveAutoTask)
@@ -103,7 +103,7 @@ public class LiveAutoTaskController extends BaseController
     /**
      * 修改直播间自动化任务配置
      */
-//    @PreAuthorize("@ss.hasPermi('shop:task:edit')")
+//    @PreAuthorize("@ss.hasPermi('live:task:edit')")
     @Log(title = "直播间自动化任务配置", businessType = BusinessType.UPDATE)
     @PutMapping
     public AjaxResult edit(@RequestBody LiveAutoTask liveAutoTask)
@@ -114,7 +114,7 @@ public class LiveAutoTaskController extends BaseController
     /**
      * 删除直播间自动化任务配置
      */
-//    @PreAuthorize("@ss.hasPermi('shop:task:remove')")
+//    @PreAuthorize("@ss.hasPermi('live:task:remove')")
     @Log(title = "直播间自动化任务配置", businessType = BusinessType.DELETE)
 	@DeleteMapping("/{ids}")
     public AjaxResult remove(@PathVariable Long[] ids)

+ 1 - 0
fs-common/src/main/java/com/fs/common/constant/LiveKeysConstant.java

@@ -9,6 +9,7 @@ public class LiveKeysConstant {
     public static final String TOTAL_VIEWS_KEY = "live:total:views:";  //累计观看人次
     public static final String MAX_ONLINE_USERS_KEY = "live:max:online:"; //最大在线人数
     public static final String ONLINE_USERS_KEY = "live:online:users:";  //当前在线人数
+    public static final String ONLINE_USERS_SET_KEY = "live:online:users:set:";  //在线用户Set(用于统计最大同时在线人数)
 
     public static final String LIVE_HOME_PAGE_LIST = "live:homePage:list"; //直播列表数据
     public static final Integer LIVE_HOME_PAGE_LIST_EXPIRE = 300; //首页缓存过期时间

+ 1 - 1
fs-company/src/main/java/com/fs/company/controller/live/LiveAutoTaskController.java

@@ -88,7 +88,7 @@ public class LiveAutoTaskController extends BaseController
     /**
      * 获取直播间自动化任务配置详细信息
      */
-    @PreAuthorize("@ss.hasPermi('live:task:query')")
+    @PreAuthorize("@ss.hasPermi('live:task:list')")
     @GetMapping(value = "/{id}")
     public AjaxResult getInfo(@PathVariable("id") Long id)
     {

+ 21 - 1
fs-live-socket/src/main/java/com/fs/live/task/Task.java

@@ -524,8 +524,28 @@ public class Task {
                             .orElse(0L)*/
                     Math.max( liveData.getUniqueViewers(), Optional.ofNullable(redisCache.increment(UNIQUE_VIEWERS_KEY + liveData.getLiveId(),0)).orElse(0L))
             );
+            // 使用Set大小来获取最大同时在线人数
+            String onlineUsersSetKey = ONLINE_USERS_SET_KEY + liveData.getLiveId();
+            Long currentSetSize = redisCache.redisTemplate.opsForSet().size(onlineUsersSetKey);
+            Long maxOnlineFromRedis = Optional.ofNullable(redisCache.getCacheObject(MAX_ONLINE_USERS_KEY + liveData.getLiveId()))
+                    .map(obj -> {
+                        if (obj instanceof Number) {
+                            return ((Number) obj).longValue();
+                        }
+                        try {
+                            return Long.parseLong(obj.toString());
+                        } catch (NumberFormatException e) {
+                            return 0L;
+                        }
+                    })
+                    .orElse(0L);
+            // 取Set大小和Redis中记录的最大在线人数的较大值
+            Long maxOnlineCount = Math.max(
+                    currentSetSize != null ? currentSetSize : 0L,
+                    maxOnlineFromRedis
+            );
             liveData.setPeakConcurrentViewers(
-                    Math.max( liveData.getPeakConcurrentViewers(), Optional.ofNullable(redisCache.increment(MAX_ONLINE_USERS_KEY + liveData.getLiveId(),0)).orElse(0L))
+                    Math.max(liveData.getPeakConcurrentViewers(), maxOnlineCount)
             );
         });
         if(!liveDatas.isEmpty())

+ 10 - 2
fs-live-socket/src/main/java/com/fs/live/websocket/service/WebSocketServer.java

@@ -102,9 +102,14 @@ public class WebSocketServer {
 
             // 记录在线人数
             redisCache.increment(ONLINE_USERS_KEY + liveId, 1);
-            Integer currentOnline = redisCache.getCacheObject(ONLINE_USERS_KEY + liveId);
-            //最大同时在线人数
+            // 将用户ID添加到在线用户Set中
+            String onlineUsersSetKey = ONLINE_USERS_SET_KEY + liveId;
+            redisCache.redisTemplate.opsForSet().add(onlineUsersSetKey, String.valueOf(userId));
+            // 获取Set的大小作为当前在线人数
+            Long currentOnlineCount = redisCache.redisTemplate.opsForSet().size(onlineUsersSetKey);
+            //最大同时在线人数 - 使用Set大小来判断
             Integer maxOnline = redisCache.getCacheObject(MAX_ONLINE_USERS_KEY + liveId);
+            int currentOnline = currentOnlineCount != null ? currentOnlineCount.intValue() : 0;
             if (maxOnline == null || currentOnline > maxOnline) {
                 redisCache.setCacheObject(MAX_ONLINE_USERS_KEY + liveId, currentOnline);
             }
@@ -197,6 +202,9 @@ public class WebSocketServer {
 
             // 直播间在线人数 -1
             redisCache.increment(ONLINE_USERS_KEY + liveId, -1);
+            // 从在线用户Set中移除用户ID
+            String onlineUsersSetKey = ONLINE_USERS_SET_KEY + liveId;
+            redisCache.redisTemplate.opsForSet().remove(onlineUsersSetKey, String.valueOf(userId));
             SendMsgVo sendMsgVo = new SendMsgVo();
             sendMsgVo.setLiveId(liveId);
             sendMsgVo.setUserId(userId);