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.
|
|
import { defineStore } from 'pinia' import Socket from '@/socket' import { useSettingStore } from './setting' import { useUserStore } from './user'
export const useWebSocketStore = defineStore({ id: 'websocket', // id必填,且需要唯一
// state
state: () => { return { // 命令websocket 实例
socketCommandInstance: null, // 事件上报websocket 实例
socketEventInstance: null, } }, // actions
actions: { initCommandSocket() { const url = 'ws://192.168.8.115:19001/' const init = new Socket(url) const settingStore = useSettingStore() const userStore = useUserStore() init.connect() init.ws.onmessage = function (ev) { console.log(JSON.parse(ev.data)) const { ackcode, messageId } = JSON.parse(ev.data)
if (ackcode == 0) { switch (messageId) { case 'getState': // 初始化完毕
settingStore.updateInitLoading() // 将sensor_data中的数据更新到store中
break case 'getAllUser': const { dbval } = JSON.parse(ev.data) userStore.updateUserList(dbval) break case 'chpasswd': break case 'startDisinfection': break case 'stopDisinfection': break case 'login': break case 'getAllSetting': const { dbval: allSetting } = JSON.parse(ev.data) settingStore.updateAllSettingList(allSetting) break default: break } } } this.socketCommandInstance = init }, sendCommandMsg(message) { this.socketCommandInstance?.msg(message) }, initEventSocket() { const url = 'ws://192.168.8.115:19002/' const init = new Socket(url) init.connect() init.ws.onmessage = function (data) { // console.log(data)
// const { command } = data
// switch (command) {
// case 'RealtimeSensorDataReport':
// const { sensor_data } = data || {}
// // 将sensor_data中的数据更新到store中
// break
// default:
// break
// }
} this.socketEventInstance = init }, sendEventMsg(message) { this.socketEventInstance?.msg(message) }, }, })
|