|
@@ -0,0 +1,60 @@
|
|
|
+import { params } from "@/env";
|
|
|
+import { ref } from "vue";
|
|
|
+import type {ResData} from '@/api'
|
|
|
+
|
|
|
+console.log('scoke 测试')
|
|
|
+const socketUrl = `wss://test-mix3d.4dkankan.com/fusion/ws/${params.caseId}`;
|
|
|
+
|
|
|
+let websocket: WebSocket | null = null;
|
|
|
+function createWebSocket() {
|
|
|
+ websocket = new WebSocket(socketUrl);
|
|
|
+ websocket.onopen = function() {
|
|
|
+ console.log('WebSocket 连接已打开');
|
|
|
+ };
|
|
|
+
|
|
|
+ websocket.onmessage = function(event) {
|
|
|
+ try {
|
|
|
+ const res = JSON.parse(event.data) as {command: string, content: ResData<any>}
|
|
|
+ switch(res.command) {
|
|
|
+ case 'ping':
|
|
|
+ break;
|
|
|
+
|
|
|
+ case 'notice':
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ } catch {
|
|
|
+ console.log('收到错误消息格式:', event.data);
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ websocket.onerror = function(event) {
|
|
|
+ console.error('WebSocket 出错:', event);
|
|
|
+ };
|
|
|
+
|
|
|
+ websocket.onclose = function(event) {
|
|
|
+ console.log('WebSocket 连接已关闭,将在 5 秒后重连');
|
|
|
+ setTimeout(createWebSocket, 5000);
|
|
|
+ };
|
|
|
+}
|
|
|
+
|
|
|
+export const notity = ref({})
|
|
|
+const handler = {
|
|
|
+ ping() {
|
|
|
+
|
|
|
+ },
|
|
|
+ notity() {
|
|
|
+
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 发送数据到服务器
|
|
|
+function sendData(command: string, message: string) {
|
|
|
+ if (websocket && websocket.readyState === WebSocket.OPEN) {
|
|
|
+ websocket.send(message);
|
|
|
+ } else {
|
|
|
+ console.log('WebSocket 连接未打开,无法发送消息');
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// 初始创建 WebSocket 连接
|
|
|
+createWebSocket();
|