| | |
| | | package com.ard.tianyu.websocket.handler; |
| | | |
| | | 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.websocket.domain.BaseMessage; |
| | | import com.ard.tianyu.websocket.domain.PullStreamDTO; |
| | | 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; |
| | | |
| | | |
| | | @Component |
| | | @Slf4j |
| | | public class TyMessageHandler extends TextWebSocketHandler { |
| | | |
| | | @Resource |
| | | private UavControlManager uavControlManager; |
| | | @Resource |
| | | private UavStreamManager uavStreamManager; |
| | | @Override |
| | | public void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception { |
| | | public void handleTextMessage(WebSocketSession session, TextMessage message) { |
| | | // 处理接收到的消息 |
| | | String userId = (String) session.getAttributes().get("userId"); |
| | | String payload = message.getPayload(); |
| | | log.info("【cmd】Received message: " + payload); |
| | | log.info("【cmd】size: " + WebSocketUtil.ONLINE_USER_SESSIONS.size()); |
| | | 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>>() { |
| | | }); |
| | | PullStreamDTO pullStreamDTO = streamMessage.getPayload(); |
| | | if (pullStreamDTO != null) { |
| | | String boxSn = pullStreamDTO.getBoxSn(); |
| | | if (pullStreamDTO.getPullType().equals(1)) { |
| | | uavStreamManager.startPullStream(boxSn, userId); |
| | | } else { |
| | | uavStreamManager.stopPullStream(boxSn, userId); |
| | | } |
| | | } |
| | | break; |
| | | } |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void afterConnectionEstablished(WebSocketSession session) { |
| | | log.info("【cmd】Connected: " + session.getId()); |
| | | log.info("【cmd】Connected: {}", session.getId()); |
| | | String userId = (String) session.getAttributes().get("userId"); |
| | | WebSocketUtil.ONLINE_USER_SESSIONS.put(userId, session); |
| | | log.info("【cmd】size: " + WebSocketUtil.ONLINE_USER_SESSIONS.size()); |
| | | log.info("【cmd】size: {}", WebSocketUtil.ONLINE_USER_SESSIONS.size()); |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void afterConnectionClosed(WebSocketSession session, CloseStatus status) { |
| | | log.info("【cmd】Disconnected: " + session.getId()); |
| | | log.info("【cmd】Disconnected: {}", session.getId()); |
| | | String userId = (String) session.getAttributes().get("userId"); |
| | | WebSocketUtil.ONLINE_USER_SESSIONS.remove(userId); |
| | | log.info("【cmd】size: " + WebSocketUtil.ONLINE_USER_SESSIONS.size()); |
| | | log.info("【cmd】size: {}", WebSocketUtil.ONLINE_USER_SESSIONS.size()); |
| | | //停止用户的所有流 |
| | | uavStreamManager.stopAllPullStreamByUser(userId); |
| | | //移除控制的无人机 |
| | | uavControlManager.releaseAllControlByUser(userId); |
| | | } |
| | | } |