将长链接剥离出来
This commit is contained in:
@@ -1,28 +1,107 @@
|
||||
import { defineStore } from "pinia";
|
||||
import { useUser } from "@/store/user.js";
|
||||
import { usePrint } from "@/store/print.js";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import useStorage from "@/utils/useStorage";
|
||||
import ReconnectingWebSocket from "reconnecting-websocket";
|
||||
|
||||
export const useSocket = defineStore({
|
||||
id: "socket",
|
||||
state: () => ({
|
||||
online: false, // 是否在线
|
||||
ws: null,
|
||||
online: false, // 在线状态
|
||||
ws: null, // websocket实例
|
||||
uuid: "", // 长连接唯一id
|
||||
heartbeatTimer: null, // 心跳计时器
|
||||
}),
|
||||
actions: {
|
||||
// 改变在线状态
|
||||
changeOnline(state) {
|
||||
this.online = state;
|
||||
// 创建uuid
|
||||
createUUID() {
|
||||
if (!useStorage.get("uuid")) {
|
||||
useStorage.set("uuid", uuidv4());
|
||||
this.uuid = useStorage.get("uuid");
|
||||
} else {
|
||||
this.uuid = useStorage.get("uuid");
|
||||
}
|
||||
},
|
||||
// 关闭ws
|
||||
close() {
|
||||
console.log("关闭ws");
|
||||
this.ws.close();
|
||||
this.ws = null;
|
||||
this.clearHeartBeat();
|
||||
},
|
||||
// 初始化
|
||||
init(wsUrl = import.meta.env.VITE_API_WSS) {
|
||||
this.createUUID();
|
||||
|
||||
const store = useUser();
|
||||
const printStore = usePrint();
|
||||
|
||||
printStore.init();
|
||||
|
||||
if (this.ws == null) {
|
||||
this.ws = new ReconnectingWebSocket(
|
||||
`${wsUrl}/shopId=${store.userInfo.shopId}/clientId=${uuid.value}`,
|
||||
null,
|
||||
{
|
||||
reconnectInterval: 10000,
|
||||
}
|
||||
);
|
||||
this.ws = new ReconnectingWebSocket(wsUrl);
|
||||
}
|
||||
|
||||
this.ws.addEventListener("open", (event) => {
|
||||
console.log("wss连接成功");
|
||||
this.online = true;
|
||||
|
||||
// 清除心跳
|
||||
this.clearHeartBeat();
|
||||
this.startheartbeat();
|
||||
|
||||
this.ws.send(
|
||||
JSON.stringify({
|
||||
type: "connect",
|
||||
shopId: store.userInfo.shopId,
|
||||
clientId: this.uuid,
|
||||
})
|
||||
);
|
||||
});
|
||||
|
||||
this.ws.addEventListener("message", (e) => {
|
||||
let data = JSON.parse(e.data);
|
||||
if (data.type == "order") {
|
||||
console.log("接收消息", data);
|
||||
this.ws.send(
|
||||
JSON.stringify({
|
||||
type: "send",
|
||||
orderNo: data.orderInfo.orderNo,
|
||||
})
|
||||
);
|
||||
// 接收订单消息,打印小票
|
||||
// printBill(data)
|
||||
|
||||
// 打印标签小票
|
||||
printStore.labelPrint(data);
|
||||
}
|
||||
});
|
||||
|
||||
this.ws.addEventListener("error", () => {
|
||||
console.log("WebSocket连接发生错误");
|
||||
this.online = false;
|
||||
this.clearHeartBeat();
|
||||
});
|
||||
|
||||
this.ws.addEventListener("error", (e) => {
|
||||
console.log("ws关闭了", e);
|
||||
this.online = false;
|
||||
this.clearHeartBeat();
|
||||
});
|
||||
},
|
||||
// 启动心跳连接
|
||||
startheartbeat() {
|
||||
this.heartbeatTimer = setInterval(() => {
|
||||
console.log("发送心跳");
|
||||
this.ws.send(JSON.stringify({ type: "heartbeat" }));
|
||||
}, 10000);
|
||||
},
|
||||
// 清除心跳
|
||||
clearHeartBeat() {
|
||||
// 清除心跳
|
||||
clearInterval(this.heartbeatTimer);
|
||||
this.heartbeatTimer = null;
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user