| | |
| | | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.alibaba.fastjson2.TypeReference; |
| | | import com.ard.tianyu.service.impl.UavControlManager; |
| | | import com.ard.tianyu.service.impl.UavStreamManager; |
| | | import com.ard.tianyu.service.control.UavControlService; |
| | | import com.ard.tianyu.service.stream.UavStreamManager; |
| | | import com.ard.tianyu.websocket.domain.BaseMessage; |
| | | import com.ard.tianyu.websocket.domain.PullStreamDTO; |
| | | import com.ard.tianyu.websocket.domain.UavStreamMessage; |
| | | import com.ard.tianyu.websocket.utils.WebSocketUtil; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.stereotype.Component; |
| | |
| | | import org.springframework.web.socket.TextMessage; |
| | | import org.springframework.web.socket.WebSocketSession; |
| | | import org.springframework.web.socket.handler.TextWebSocketHandler; |
| | | |
| | | import javax.annotation.Resource; |
| | | |
| | | |
| | |
| | | public class TyMessageHandler extends TextWebSocketHandler { |
| | | |
| | | @Resource |
| | | private UavControlManager uavControlManager; |
| | | @Resource |
| | | private UavStreamManager uavStreamManager; |
| | | @Resource |
| | | private UavControlService uavControlService; |
| | | |
| | | @Override |
| | | public void handleTextMessage(WebSocketSession session, TextMessage message) { |
| | | // 处理接收到的消息 |
| | | String userId = (String) session.getAttributes().get("userId"); |
| | | String userIdStr = (String) session.getAttributes().get("userId"); |
| | | Long userId = Long.parseLong(userIdStr); |
| | | String payload = message.getPayload(); |
| | | log.info("【cmd】Received message: {}", payload); |
| | | log.info("【cmd】size: {}", WebSocketUtil.ONLINE_USER_SESSIONS.size()); |
| | | BaseMessage<?> baseMessage = JSON.parseObject(payload, BaseMessage.class); |
| | | switch (baseMessage.getType()) { |
| | | case "stream": |
| | | BaseMessage<PullStreamDTO> streamMessage = JSON.parseObject(payload, |
| | | new TypeReference<BaseMessage<PullStreamDTO>>() { |
| | | BaseMessage<UavStreamMessage> streamMessage = JSON.parseObject(payload, |
| | | new TypeReference<BaseMessage<UavStreamMessage>>() { |
| | | }); |
| | | PullStreamDTO pullStreamDTO = streamMessage.getPayload(); |
| | | if (pullStreamDTO != null) { |
| | | String boxSn = pullStreamDTO.getBoxSn(); |
| | | if (pullStreamDTO.getPullType().equals(1)) { |
| | | UavStreamMessage uavStreamMessage = streamMessage.getPayload(); |
| | | if (uavStreamMessage != null) { |
| | | String boxSn = uavStreamMessage.getBoxSn(); |
| | | if (uavStreamMessage.getPullType().equals(1)) { |
| | | uavStreamManager.startPullStream(boxSn, userId); |
| | | } else { |
| | | uavStreamManager.stopPullStream(boxSn, userId); |
| | |
| | | @Override |
| | | public void afterConnectionEstablished(WebSocketSession session) { |
| | | log.info("【cmd】Connected: {}", session.getId()); |
| | | String userId = (String) session.getAttributes().get("userId"); |
| | | String userIdStr = (String) session.getAttributes().get("userId"); |
| | | Long userId = Long.parseLong(userIdStr); |
| | | WebSocketUtil.ONLINE_USER_SESSIONS.put(userId, session); |
| | | log.info("【cmd】size: {}", WebSocketUtil.ONLINE_USER_SESSIONS.size()); |
| | | |
| | |
| | | @Override |
| | | public void afterConnectionClosed(WebSocketSession session, CloseStatus status) { |
| | | log.info("【cmd】Disconnected: {}", session.getId()); |
| | | String userId = (String) session.getAttributes().get("userId"); |
| | | String userIdStr = (String) session.getAttributes().get("userId"); |
| | | Long userId = Long.parseLong(userIdStr); |
| | | WebSocketUtil.ONLINE_USER_SESSIONS.remove(userId); |
| | | log.info("【cmd】size: {}", WebSocketUtil.ONLINE_USER_SESSIONS.size()); |
| | | //停止用户的所有流 |
| | | uavStreamManager.stopAllPullStreamByUser(userId); |
| | | //移除控制的无人机 |
| | | uavControlManager.releaseAllControlByUser(userId); |
| | | if (userId != null) { |
| | | // 释放该用户所有 UAV 控制权 |
| | | uavControlService.releaseAllControls(userId); |
| | | } |
| | | } |
| | | } |