| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- import { defineStore } from 'pinia';
- import { ref, computed } from 'vue';
- import client from "@/stores/modules/client";
- import icpList from '@/utils/ipc'
- const socket = defineStore('socket', ()=>{
- const socket = ref(null);
- const clientStore = client();
- clientStore.ipc.on(icpList.socket.connect, async (event, result) => {
- socket.value = null
- })
- async function connectSocket(){
- if(socket.value) return socket.value;
- await new Promise((resolve, reject) => {
- if(!clientStore.isClient) resolve(true);
- clientStore.ipc.removeAllListeners(icpList.socket.connect);
- clientStore.ipc.send(icpList.socket.connect);
- clientStore.ipc.on(icpList.socket.connect+'_open', async (event, result) => {
- socket.value = result;
- resolve(result);
- });
- });
- }
- async function disconnectSocket(){
- if(!clientStore.isClient) return;
- await new Promise((resolve,reject)=>{
- clientStore.ipc.removeAllListeners(icpList.socket.disconnect);
- clientStore.ipc.send(icpList.socket.disconnect);
- clientStore.ipc.on(icpList.socket.connect, async (event, result) => {
- socket.value = null
- resolve(result)
- })
- })
- }
- async function sendMessage(data){
- if(!clientStore.isClient) return;
- if (data?.type !== 'get_scale_data') {
- console.log('sendMessage');
- console.log(data);
- }
- new Promise((resolve,reject)=>{
- clientStore.ipc.removeAllListeners(icpList.socket.sendMessage);
- clientStore.ipc.send(icpList.socket.sendMessage,JSON.stringify(data));
- })
- }
- return {
- sendMessage,
- connectSocket,
- disconnectSocket,
- }
- });
- export default socket;
|