This commit is contained in:
gyq
2024-07-12 17:54:26 +08:00
parent 815b6e0a25
commit e00feb82ec
4 changed files with 32 additions and 41 deletions

View File

@@ -1,3 +1,4 @@
import _ from "lodash";
import { defineStore } from "pinia";
import { useUser } from "@/store/user.js";
import { usePrint } from "@/store/print.js";
@@ -31,10 +32,19 @@ export const useSocket = defineStore({
// 关闭ws
close() {
console.log("关闭ws");
this.ws.close();
this.ws = null;
this.ws.close(1000);
// this.ws = null;
this.clearHeartBeat();
},
wsReconnect: _.throttle(
function () {
if (this.ws.readyState == ReconnectingWebSocket.OPEN) return;
this.ws.reconnect();
console.log("11111");
},
2000,
{ leading: true, trailing: false }
),
// 初始化
init(wsUrl = import.meta.env.VITE_API_WSS) {
this.createUUID();
@@ -45,7 +55,11 @@ export const useSocket = defineStore({
printStore.init();
if (this.ws == null) {
console.log("创建新的ws连接");
this.ws = new ReconnectingWebSocket(wsUrl);
} else {
console.log("重新连接ws");
this.wsReconnect();
}
this.ws.addEventListener("open", (event) => {