| | |
| | | package com.ard.sample.component.websocket.handler; |
| | | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.ard.sample.common.model.ResponseResult; |
| | | 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 org.checkerframework.checker.units.qual.A; |
| | | import com.ard.sample.manage.service.IControlService; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.stereotype.Component; |
| | | import org.springframework.web.socket.CloseStatus; |
| | |
| | | import java.util.*; |
| | | import java.util.concurrent.ConcurrentHashMap; |
| | | |
| | | @Slf4j |
| | | @Component |
| | | public class DockWebSocketHandler extends TextWebSocketHandler { |
| | | |
| | | @Autowired |
| | | private LiveStreamHandler _liveStreamService; |
| | | |
| | | @Autowired |
| | | private IControlService _controlService; |
| | | |
| | | // 存储用户会话:userId -> WebSocketSession |
| | | private final Map<String, WebSocketSession> userSessions = new ConcurrentHashMap<>(); |
| | |
| | | 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()); |
| | | } |
| | | |
| | | /** |
| | |
| | | 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)){ |
| | | // 开启直播 |
| | | // 开启直播逻辑 |
| | |
| | | _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); |
| | | 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); |
| | | } |
| | | } |
| | | } |
| | |
| | | if (userId != null) { |
| | | userSessions.remove(userId); |
| | | sessionLocks.remove(session); // 移除会话锁 |
| | | // 关闭远程调试模式,暂时写死sn,后续改为动态获取sn |
| | | ResponseResult responseResult = _controlService.controlDock("7CTDLCE00A1UPE","debug_mode_close",null); |
| | | log.info("关闭远程调试模式"); |
| | | if(responseResult.getCode() == ResponseResult.CODE_SUCCESS){ |
| | | log.info("关闭远程调试模式成功"); |
| | | } |
| | | // 清除该用户的所有订阅 |
| | | removeUserAllSubscriptions(userId); |
| | | System.out.println("用户[" + userId + "]已断开连接,当前在线人数:" + userSessions.size()); |
| | | //System.out.println("用户[" + userId + "]已断开连接,当前在线人数:" + userSessions.size()); |
| | | log.info("用户[{}]已断开连接,当前在线人数:{}", userId, userSessions.size()); |
| | | } |
| | | } |
| | | |
| | |
| | | */ |
| | | private void removeUserAllSubscriptions(String userId) { |
| | | for (Map.Entry<String, Set<String>> entry : subscriptions.entrySet()) { |
| | | // 停止直播逻辑 |
| | | 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()); |
| | | } |
| | | } |
| | |
| | | } |
| | | |
| | | 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); |
| | |
| | | // } |
| | | } |
| | | } catch (IOException e) { |
| | | System.err.println("发送消息错误:" + e.getMessage()); |
| | | // System.err.println("发送消息错误:" + e.getMessage()); |
| | | log.error("发送消息错误:{}", e.getMessage()); |
| | | } |
| | | } |
| | | |