dreamworks 前端vue3+vite项目开发模板
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

40 lines
1.0 KiB

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)
},
},
})