From 4e9465a4608f569e6d7a91048e41cb5aac3828fd Mon Sep 17 00:00:00 2001
From: zhaowenyu <35589726@qq.com>
Date: Fri, 07 Nov 2025 16:39:42 +0800
Subject: [PATCH] 2025-11-7 zwy 飞行器视频增加video_type 直播视频流镜头类型
---
ard-sample/src/main/java/com/ard/sample/component/websocket/handler/DockWebSocketHandler.java | 50 ++++++++++++++++++++++++++++++++------------------
1 files changed, 32 insertions(+), 18 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 98c1618..89ccdd4 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
@@ -1,8 +1,12 @@
package com.ard.sample.component.websocket.handler;
import com.alibaba.fastjson2.JSON;
+import com.ard.sample.component.websocket.enums.MessageType;
import com.ard.sample.component.websocket.model.Payload;
import com.ard.sample.component.websocket.model.WebSocketMessage;
+import lombok.extern.slf4j.Slf4j;
+import org.checkerframework.checker.units.qual.A;
+import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.CloseStatus;
import org.springframework.web.socket.TextMessage;
@@ -13,8 +17,12 @@
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
+@Slf4j
@Component
public class DockWebSocketHandler extends TextWebSocketHandler {
+
+ @Autowired
+ private LiveStreamHandler _liveStreamService;
// 存储用户会话:userId -> WebSocketSession
private final Map<String, WebSocketSession> userSessions = new ConcurrentHashMap<>();
@@ -40,7 +48,8 @@
userSessions.put(userId, session);
// 为新会话初始化锁对象
sessionLocks.put(session, new Object());
- System.out.println("用户[" + userId + "]已连接,当前在线人数:" + userSessions.size());
+ //System.out.println("用户[" + userId + "]已连接,当前在线人数:" + userSessions.size());
+ log.info("用户[{}]已连接,当前在线人数:{}", userId, userSessions.size());
}
/**
@@ -58,11 +67,11 @@
Payload payload = webSocketMessage.getPayload();
// 验证消息类型是否合法
- if (!Arrays.asList("status", "control", "progress").contains(type)) {
+ //if (!Arrays.asList("status", "control", "progress", "state").contains(type)) {
+ if(!MessageType.fromString(type)){
session.sendMessage(new TextMessage("不支持的消息类型:" + type));
return;
}
-
// 处理订阅/取消订阅
handleSubscription(userId, type, payload);
@@ -78,20 +87,34 @@
private void handleSubscription(String userId, String type, Payload payload) {
String sn = payload.getSn();
boolean subscribe = payload.isSubscribe();
+ String videoType = payload.getVideotype();
String subscriptionKey = getSubscriptionKey(sn, type); // 生成唯一订阅标识
if (subscribe) {
// 订阅操作
subscriptions.computeIfAbsent(subscriptionKey, k -> ConcurrentHashMap.newKeySet()).add(userId);
- System.out.println("用户[" + userId + "]订阅了 设备[" + sn + "]的[" + type + "]类型消息");
+ //System.out.println("用户[" + userId + "]订阅了 设备[" + sn + "]的[" + type + "]类型消息");
+ log.info("用户[{}]订阅了 设备[{}]的[{}]类型消息", userId, sn, type);
+ if(MessageType.LIVESTREAM.getType().equals(type)){
+ // 开启直播
+ // 开启直播逻辑
+ System.out.println("开启直播");
+ _liveStreamService.StartLiveStreaming(sn, videoType);
+ }
} else {
+ // 停止直播
+ if(MessageType.LIVESTREAM.getType().equals(type)){
+ System.out.println("停止直播");
+ _liveStreamService.StopLiveStreaming(sn);
+ }
// 取消订阅操作
if (subscriptions.containsKey(subscriptionKey)) {
subscriptions.get(subscriptionKey).remove(userId);
if (subscriptions.get(subscriptionKey).isEmpty()) {
subscriptions.remove(subscriptionKey);
}
- System.out.println("用户[" + userId + "]取消订阅了 设备[" + sn + "]的[" + type + "]类型消息");
+ //System.out.println("用户[" + userId + "]取消订阅了 设备[" + sn + "]的[" + type + "]类型消息");
+ log.info("用户[{}]取消订阅了 设备[{}]的[{}]类型消息", userId, sn, type);
}
}
}
@@ -107,7 +130,8 @@
sessionLocks.remove(session); // 移除会话锁
// 清除该用户的所有订阅
removeUserAllSubscriptions(userId);
- System.out.println("用户[" + userId + "]已断开连接,当前在线人数:" + userSessions.size());
+ //System.out.println("用户[" + userId + "]已断开连接,当前在线人数:" + userSessions.size());
+ log.info("用户[{}]已断开连接,当前在线人数:{}", userId, userSessions.size());
}
}
@@ -136,17 +160,6 @@
}
try {
- // 构建发送的消息
-// WebSocketMessage message = new WebSocketMessage();
-// message.setType(type);
-//
-// Payload payload = new Payload();
-// payload.setSn(sn);
-// payload.setSubscribe(true); // 仅作为标识,无实际意义
-// message.setPayload(payload);
-//
-// String jsonMessage = JSON.toJSONString(message);
-
// 向所有订阅者发送消息
for (String userId : subscriberIds) {
WebSocketSession session = userSessions.get(userId);
@@ -176,7 +189,8 @@
// }
}
} catch (IOException e) {
- System.err.println("发送消息错误:" + e.getMessage());
+// System.err.println("发送消息错误:" + e.getMessage());
+ log.error("发送消息错误:{}", e.getMessage());
}
}
--
Gitblit v1.9.3