| | |
| | | import com.alibaba.fastjson2.JSON; |
| | | import com.alibaba.fastjson2.JSONObject; |
| | | import com.ard.work.component.websocket.WebSocketSessionManager; |
| | | import com.ard.work.domain.message.BaseMessage; |
| | | import lombok.extern.slf4j.Slf4j; |
| | | import org.springframework.web.socket.TextMessage; |
| | | import org.springframework.web.socket.WebSocketSession; |
| | |
| | | WebSocketSessionManager.broadcast(JSON.toJSONString(message)); |
| | | } |
| | | |
| | | // 发送消息到所有 WebSocket 客户端 |
| | | public static <T> void sendToAll(BaseMessage<T> baseMessage) { |
| | | WebSocketUtil.sendMessageAll(baseMessage); // 你已有的 WebSocket 发送逻辑 |
| | | } |
| | | |
| | | // 根据消息类型和内容创建消息并发送 |
| | | public static <T> void sendMessage(String type, T payload) { |
| | | BaseMessage<T> baseMessage = BaseMessage.<T>builder() |
| | | .type(type) |
| | | .payload(payload) |
| | | .build(); |
| | | sendToAll(baseMessage); |
| | | } |
| | | } |