From 677748a0508a91f9423cae558f1bdee5d83ac131 Mon Sep 17 00:00:00 2001
From: zhaowenyu <35589726@qq.com>
Date: Wed, 12 Nov 2025 17:52:28 +0800
Subject: [PATCH] 2025-11-8 zwy 优化websocket代码,在最后一个订阅视频的用户断开后,机场停止视频直播
---
ard-sample/src/main/java/com/ard/sample/component/websocket/handler/DockWebSocketHandler.java | 20 +++++++++++---------
ard-sample/src/main/java/com/ard/sample/component/websocket/handler/LiveStreamHandler.java | 5 ++---
2 files changed, 13 insertions(+), 12 deletions(-)
diff --git a/ard-sample/src/main/java/com/ard/sample/component/websocket/handler/DockWebSocketHandler.java b/ard-sample/src/main/java/com/ard/sample/component/websocket/handler/DockWebSocketHandler.java
index a6f4066..e682685 100644
--- a/ard-sample/src/main/java/com/ard/sample/component/websocket/handler/DockWebSocketHandler.java
+++ b/ard-sample/src/main/java/com/ard/sample/component/websocket/handler/DockWebSocketHandler.java
@@ -100,11 +100,12 @@
_liveStreamService.StartLiveStreaming(sn);
}
} else {
- // 停止直播
- if(MessageType.LIVESTREAM.getType().equals(type)){
- System.out.println("停止直播");
- _liveStreamService.StopLiveStreaming(sn);
- }
+ // 停止直播--前端不主动关注直播,则在断开连接时停止
+// if(MessageType.LIVESTREAM.getType().equals(type)){
+// System.out.println("停止直播");
+//
+// _liveStreamService.StopLiveStreaming(sn);
+// }
// 取消订阅操作
if (subscriptions.containsKey(subscriptionKey)) {
subscriptions.get(subscriptionKey).remove(userId);
@@ -138,12 +139,13 @@
*/
private void removeUserAllSubscriptions(String userId) {
for (Map.Entry<String, Set<String>> entry : subscriptions.entrySet()) {
- if (entry.getKey().contains(MessageType.LIVESTREAM.getType())) {
- String sn = entry.getKey().split(":")[0];
- _liveStreamService.StopLiveStreaming(sn);
- }
+ // 停止直播逻辑
entry.getValue().remove(userId);
if (entry.getValue().isEmpty()) {
+ if (entry.getKey().contains(MessageType.LIVESTREAM.getType())) {
+ String sn = entry.getKey().split(":")[0];
+ _liveStreamService.StopLiveStreaming(sn);
+ }
subscriptions.remove(entry.getKey());
}
}
diff --git a/ard-sample/src/main/java/com/ard/sample/component/websocket/handler/LiveStreamHandler.java b/ard-sample/src/main/java/com/ard/sample/component/websocket/handler/LiveStreamHandler.java
index a455341..d24ac28 100644
--- a/ard-sample/src/main/java/com/ard/sample/component/websocket/handler/LiveStreamHandler.java
+++ b/ard-sample/src/main/java/com/ard/sample/component/websocket/handler/LiveStreamHandler.java
@@ -53,11 +53,10 @@
rlive.setType(MessageType.LIVESTREAM.getType());
resultLive = _objectMapper.writeValueAsString(rlive);
RedisOpsUtils.set(RedisConst.WEBSOCKET_VIDEO_ADDRESS_PREFIX + sn, resultLive);
-
- _dockWebSocketHandler.sendMessageToSubscribers(sn, MessageType.LIVESTREAM.getType(), resultLive);
- log.info("SN: {}-直播地址:{}", sn, resultLive);
}
}
+ _dockWebSocketHandler.sendMessageToSubscribers(sn, MessageType.LIVESTREAM.getType(), resultLive);
+ log.info("SN: {}-直播地址:{}", sn, resultLive);
} catch (Exception e) {
log.info("SN: {}-开启直播失败!原因:{}", sn, e.getMessage());
e.printStackTrace();
--
Gitblit v1.9.3