From a68b7f0d46a830f338217c72c22d7e63d219e5b4 Mon Sep 17 00:00:00 2001
From: zjj <3272660260@qq.com>
Date: 星期一, 17 二月 2025 14:41:12 +0800
Subject: [PATCH] #移库任务
---
zy-asrs-flow/src/pages/map/websocket.js | 115 +++++++++++++++++++++++++++++++++++++++------------------
1 files changed, 78 insertions(+), 37 deletions(-)
diff --git a/zy-asrs-flow/src/pages/map/websocket.js b/zy-asrs-flow/src/pages/map/websocket.js
index 20dea9b..39b9eba 100644
--- a/zy-asrs-flow/src/pages/map/websocket.js
+++ b/zy-asrs-flow/src/pages/map/websocket.js
@@ -1,42 +1,83 @@
-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 30 seconds
+ }
- // 鍑虹幇閿欒鏃惰Е鍙�
- webSocket.onerror = function (event) {
- console.error('WebSocket error observed:', event);
- // 鍦ㄨ繖閲屽鐞哤ebSocket閿欒
- };
-
- // 杩炴帴鍏抽棴鏃惰Е鍙�
- 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
- };
-};
\ No newline at end of file
+ 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.');
+ }
+ }
+
+ // Override
+ onMessage(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;
+ }
+ }
+}
\ No newline at end of file
--
Gitblit v1.9.1