优化长连接

This commit is contained in:
gyq
2024-07-08 09:22:40 +08:00
parent de8c5e20f4
commit d101ecea41
5 changed files with 68 additions and 211 deletions

View File

@@ -1,14 +1,28 @@
import { defineStore } from "pinia";
import ReconnectingWebSocket from "reconnecting-websocket";
export const useSocket = defineStore({
id: "socket",
state: () => ({
online: false,
online: false, // 是否在线
ws: null,
}),
actions: {
// 登录
// 改变在线状态
changeOnline(state) {
this.online = state;
},
// 初始化
init(wsUrl = import.meta.env.VITE_API_WSS) {
if (this.ws == null) {
this.ws = new ReconnectingWebSocket(
`${wsUrl}/shopId=${store.userInfo.shopId}/clientId=${uuid.value}`,
null,
{
reconnectInterval: 10000,
}
);
}
},
},
});