12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- import { useUserStore } from "@/pinia/user-store";
- import env from "@/utils/setting";
- import { useCySocket } from "@/utils/useCySocket";
- import { UseWebSocketReturn } from "@vueuse/core";
- let webSocket: UseWebSocketReturn<any> | null = null;
- let globalCallback = new Map();
- export function closeWebSocket() {
- if (webSocket !== null) {
- webSocket.close();
- webSocket = null;
- }
- }
- export function setCallback(messageName, callback) {
- globalCallback.set(messageName, callback);
- }
- export function sendAMessage(name, data) {
- if (globalCallback.has(name)) {
- try {
- globalCallback.get(name)(data);
- } catch {}
- }
- }
- export const socketErrDialog = ref(false);
- export function initWebSocket() {
- const userStore = useUserStore().userInfo;
- const url = `${env.VITE_SOCKET_V2}/intergrationPlatform/${userStore.code}`;
- // const url = `${env.VITE_SOCKET_URL}/02896-172.16.30.66-a4f2eddf9e114c7dad5a9473a3d75020fAhr5UY4`;
- webSocket = useCySocket(url, {
- setInfo: false,
- heartbeat: {
- pongTimeout: 1000,
- message: "heart-beat",
- interval: 1000 * 60,
- },
- onMessage: (ws, e) => {
- const data = JSON.parse(e.data);
- sendAMessage(data.name || data.code, data.message);
- },
- });
- }
- export function sendMeg(code: string, data = null) {
- webSocket &&
- webSocket.send(
- JSON.stringify({
- code,
- data,
- })
- );
- }
|