import { defineStore } from 'pinia' import Socket from '@/socket' export const useWebSocketStore = defineStore({ id: 'websocket', // id必填,且需要唯一 // state state: () => { return { // 命令websocket 实例 socketCommandInstance: null, // 事件上报websocket 实例 socketEventInstance: null, } }, // actions actions: { initCommandSocket() { const url = import.meta.env.VITE_BASE_WS1_URL const init = new Socket(url) init.connect() init.ws.onmessage = function (ev) {} this.socketCommandInstance = init }, sendCommandMsg(message) { this.socketCommandInstance?.msg(message) }, initEventSocket() { const url = import.meta.env.VITE_BASE_WS2_URL const init = new Socket(url) init.connect() init.ws.onmessage = function (ev) { // console.log(JSON.parse(ev.data)) } this.socketEventInstance = init }, sendEventMsg(message) { this.socketEventInstance?.msg(message) }, }, })