zy-asrs-flow/src/config/setting.ts | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
zy-asrs-flow/src/pages/map/index.jsx | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
zy-asrs-flow/src/pages/map/websocket.js | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/map/websocket/MapWebSocket.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 | |
zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/map/websocket/MapWebsocket.java | ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史 |
zy-asrs-flow/src/config/setting.ts
@@ -1,5 +1,6 @@ // 接口地址 export const API_BASE_URL: string = 'http://127.0.0.1:9090/wcs'; export const WEBSOCKET_BASE_URL: string = 'ws://127.0.0.1:9090/wcs'; export const API_TIMEOUT: number = 60; zy-asrs-flow/src/pages/map/index.jsx
@@ -8,14 +8,13 @@ FileAddOutlined, CompressOutlined, SettingOutlined, CloseOutlined } from '@ant-design/icons'; import './index.css' import { createStyles } from 'antd-style'; import Edit from './components/device'; import Settings from './components/settings' import * as Utils from './utils' import * as Websocket from './websocket' import WebSocketClient from './websocket' import Player from './player'; import MapSearch from './header/search'; import MapFloor from './header/floor'; @@ -61,7 +60,7 @@ SETTINGS_MODEL: "3", }) let player; let player, websocket; const Map = () => { const intl = useIntl(); @@ -104,6 +103,9 @@ Utils.syncMapContainer(player.mapContainer); Utils.syncNotify(notify); websocket = new WebSocketClient('/ws/map/websocket'); websocket.connect(); const handleResize = () => { setWindowSize({ width: window.innerWidth, @@ -123,8 +125,6 @@ player.adaptScreen(); Utils.mapNotify(intl.formatMessage({ id: 'map.load.success', defaultMessage: '欢迎使用WCS系统' })); }, 200) Websocket.createWs(); } initialize(); }, []); zy-asrs-flow/src/pages/map/websocket.js
@@ -1,42 +1,84 @@ export const createWs = () => { console.log('Creating WebSocket connection...'); const webSocket = new WebSocket('ws://127.0.0.1:9090/wcs/map/websocket'); import { WEBSOCKET_BASE_URL } from '@/config/setting'; // 连接打开时触发 webSocket.onopen = function (event) { console.log('WebSocket connection opened:', event); // 在此添加可以在连接打开时发送的任何消息 // webSocket.send('Hello Server!'); }; export default class WebSocketClient { // 从服务器接收到消息时触发 webSocket.onmessage = function (event) { console.log('WebSocket message received:', event.data); // 在这里处理接收到的消息 }; constructor(path) { this.url = WEBSOCKET_BASE_URL + path; this.webSocket = null; this.heartbeatInterval = null; // Store the interval ID this.heartbeatFrequency = 30000; // Heartbeat every 10 seconds } // 出现错误时触发 webSocket.onerror = function (event) { console.error('WebSocket error observed:', event); // 在这里处理WebSocket错误 }; // 连接关闭时触发 webSocket.onclose = function (event) { console.log('WebSocket connection closed:', event); // 在这里处理连接关闭事件 // 你可能想要重新连接或对用户进行友好提示 }; // 请求关闭WebSocket连接(主动关闭),你可以从其他函数调用这个 const closeWebSocket = () => { if (webSocket.readyState === WebSocket.OPEN) { webSocket.close(); // 这将触发'onclose'事件 connect() { if (!this.url) { console.error('WebSocketClient: Cannot connect without url.'); return; } }; return { webSocket, closeWebSocket }; }; this.webSocket = new WebSocket(this.url); this.webSocket.onopen = (event) => { console.log('websocket connection opened.'); // Start the heartbeat this.startHeartbeat(); }; this.webSocket.onmessage = (event) => { console.log('websocket message received:', event.data); this.onMessage(event.data); }; this.webSocket.onerror = (event) => { console.error('websocket error observed:', event); }; this.webSocket.onclose = (event) => { console.log('websocket connection closed!'); // Clear the heartbeat this.stopHeartbeat(); this.reconnect(); }; } sendMessage(message) { if (this.webSocket && this.webSocket.readyState === WebSocket.OPEN) { this.webSocket.send(message); } else { console.error('WebSocketClient: Cannot send message, WebSocket connection is not open.'); } } onMessage(data) { // 子类可能会重写这个方法来处理消息 console.log('WebSocketClient: Message received:', data); } close() { if (this.webSocket && this.webSocket.readyState === WebSocket.OPEN) { this.webSocket.close(); } } reconnect() { setTimeout(() => { console.log('WebSocketClient: Attempting to reconnect...'); this.connect(); }, 3000); } startHeartbeat() { if(this.heartbeatInterval) { clearInterval(this.heartbeatInterval); } this.heartbeatInterval = setInterval(() => { this.sendMessage('1'); }, this.heartbeatFrequency); } stopHeartbeat() { if (this.heartbeatInterval) { clearInterval(this.heartbeatInterval); this.heartbeatInterval = null; } } } zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/map/websocket/MapWebSocket.java
New file @@ -0,0 +1,72 @@ package com.zy.asrs.wcs.core.map.websocket; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Component; import javax.websocket.*; import javax.websocket.server.ServerEndpoint; import java.util.concurrent.ConcurrentHashMap; @Slf4j @Component @ServerEndpoint(value = "/ws/map/websocket") public class MapWebSocket { private static final long MAX_IDLE_TIMEOUT = 120 * 1000; private static final ConcurrentHashMap<String, Session> sessions = new ConcurrentHashMap<>(); @OnOpen public void onOpen(Session session) { session.setMaxIdleTimeout(MAX_IDLE_TIMEOUT); sessions.put(session.getId(), session); log.info("Opened new session in instance " + session.getId()); } @OnMessage public void onMessage(String message, Session session) { if (!message.equals("1")) { log.info("Received WebSocket message: {}", message); } } @OnClose public void onClose(Session session) { sessions.remove(session.getId()); log.info("Closed WebSocket session: {}", session.getId()); } @OnError public void onError(Session session, Throwable error) { error.printStackTrace(); log.error("Error in WebSocket session: {}", session.getId(), error); try { session.close(); } catch (Exception e) { log.error("Error closing session: {}", session.getId(), e); } finally { sessions.remove(session.getId()); } } public static void sendMessage(Session session, String message) { if(session.isOpen()){ try { session.getBasicRemote().sendText(message); } catch (Exception e) { log.error("Failed to send message to session: {}", session.getId(), e); } } } public static void broadcast(String message) { sessions.forEach((id, session) -> { try { session.getBasicRemote().sendText(message); } catch (Exception e) { log.error("Failed to send message to session: {}", id, e); } }); } } zy-asrs-wcs/src/main/java/com/zy/asrs/wcs/core/map/websocket/MapWebsocket.java
File was deleted