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 = 'ws://192.168.1.100:19001/' const init = new Socket(url) init.connect() init.ws.onmessage = function (ev) { console.log(ev) // switch (data.type) { // case 1: // break // case 2: // break // } } this.socketCommandInstance = init }, sendCommandMsg(message) { this.socketCommandInstance?.msg(message) }, initEventSocket() { const url = 'ws://192.168.1.100:19002/' const init = new Socket(url) init.connect() init.ws.onmessage = function (ev) { console.log(ev) // switch (data.type) { // case 1: // break // case 2: // break // } } this.socketEventInstance = init }, sendEventMsg(message) { this.socketEventInstance?.msg(message) }, }, })