diff --git a/src/components/Common/Bus/index.js b/src/components/Common/Bus/index.js
new file mode 100644
index 0000000..b0230b5
--- /dev/null
+++ b/src/components/Common/Bus/index.js
@@ -0,0 +1,2 @@
+import Vue from 'vue'
+export default new Vue()
diff --git a/src/components/Common/Websocket/index.js b/src/components/Common/Websocket/index.js
new file mode 100644
index 0000000..11767f9
--- /dev/null
+++ b/src/components/Common/Websocket/index.js
@@ -0,0 +1,131 @@
+export default class MyWebSocket {
+ /**
+ * 创建 WebSocket 实例
+ * @param {string} path - WebSocket 服务路径
+ * @param {Function} messageCallback - 消息处理回调函数 (error, data) => void
+ */
+ constructor(path, messageCallback) {
+ // 拼接完整 WebSocket 地址
+ const baseUrl = process.env.VUE_APP_WEBSOCKET_URL
+ this.url = new URL(path, baseUrl).href.replace(/^http/, 'ws')
+ this.messageCallback = messageCallback
+ this.reconnectAttempts = 0
+ this.maxReconnectAttempts = 5
+ this.reconnectInterval = 2000
+ this.messageQueue = []
+ this.connectionPromise = null
+
+ this._connect()
+ }
+
+ /** 建立 WebSocket 连接 */
+ _connect() {
+ this.ws = new WebSocket(this.url)
+
+ this.ws.addEventListener('open', () => {
+ this.reconnectAttempts = 0 // 重置重连计数
+ this._flushQueue() // 发送队列中的消息
+ })
+
+ this.ws.addEventListener('message', (event) => {
+ try {
+ const data = JSON.parse(event.data)
+ this.messageCallback(null, data)
+ } catch (error) {
+ this.messageCallback(new Error(`消息解析失败: ${error.message} | 原始数据: ${event.data}`), null)
+ }
+ })
+
+ this.ws.addEventListener('error', (error) => {
+ this.messageCallback(new Error(`连接错误: ${error.message || '未知错误'}`), null)
+ })
+
+ this.ws.addEventListener('close', (event) => {
+ // 非正常关闭时尝试重连
+ if (event.code !== 1000 && this.reconnectAttempts < this.maxReconnectAttempts) {
+ setTimeout(() => {
+ this.reconnectAttempts++
+ this._connect()
+ }, this.reconnectInterval)
+ }
+ })
+ }
+
+ /** 发送队列中缓存的消息 */
+ _flushQueue() {
+ while (this.messageQueue.length > 0 && this.isConnected()) {
+ const message = this.messageQueue.shift()
+ this._sendInternal(message)
+ }
+ }
+
+ /** 内部发送方法 */
+ _sendInternal(data) {
+ if (!this.isConnected()) return
+
+ try {
+ const payload = JSON.stringify(data)
+ this.ws.send(payload)
+ } catch (error) {
+ this.messageCallback(new Error(`消息发送失败: ${error.message}`), null)
+ }
+ }
+
+ /**
+ * 检查连接状态
+ * @returns {boolean} 是否已连接
+ */
+ isConnected() {
+ return this.ws?.readyState === WebSocket.OPEN
+ }
+
+ /**
+ * 发送消息
+ * @param {Object} data - 要发送的数据
+ */
+ send(data) {
+ if (this.isConnected()) {
+ this._sendInternal(data)
+ } else {
+ // 连接未就绪时缓存消息
+ this.messageQueue.push(data)
+
+ // 首次连接时创建连接等待
+ if (!this.connectionPromise) {
+ this.connectionPromise = new Promise((resolve) => {
+ const check = () => {
+ if (this.isConnected()) {
+ resolve()
+ } else {
+ setTimeout(check, 100)
+ }
+ }
+ check()
+ })
+ }
+ }
+ }
+
+ /** 关闭连接 */
+ close() {
+ if (this.ws) {
+ this.ws.close(1000, '用户主动关闭')
+ this.reconnectAttempts = this.maxReconnectAttempts // 禁止重连
+ }
+ }
+}
+
+// // 使用示例
+// const ws = new MyWebSocket('/notification', (err, data) => {
+// if (err) {
+// console.error('WebSocket 错误:', err);
+// return;
+// }
+// console.log('收到消息:', data);
+// });
+
+// // 发送消息
+// ws.send({ type: 'ping', timestamp: Date.now() });
+
+// // 关闭连接(在组件销毁时调用)
+// // ws.close();
diff --git a/src/components/Common/register.js b/src/components/Common/register.js
index 0f45927..9fc907d 100644
--- a/src/components/Common/register.js
+++ b/src/components/Common/register.js
@@ -10,7 +10,9 @@ import WangEditor from './WangEditor/Index.vue'
import Loading from './Directives/Loading'
+import Bus from './Bus/index'
import MyCesium from './Cesium/index'
+import MyWebsocket from './Websocket/index'
export default {
install(Vue) {
@@ -26,6 +28,8 @@ export default {
Vue.directive('loading', Loading)
+ window.$bus = Bus
window.MyCesium = MyCesium
+ window.MyWebsocket = MyWebsocket
},
}
diff --git a/src/views/simulationScene/sceneEditing/index.vue b/src/views/simulationScene/sceneEditing/index.vue
index 7dd7a59..0858df5 100644
--- a/src/views/simulationScene/sceneEditing/index.vue
+++ b/src/views/simulationScene/sceneEditing/index.vue
@@ -48,9 +48,9 @@