You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

42 lines
1.0 KiB

1 year ago
import * as signalR from "@microsoft/signalr";
import PubSub from "pubsub-js";
import useAppStore from "../store/app.js";
import { isLogin } from "../api/user.js";
let connectionId = null;
const connection = new signalR.HubConnectionBuilder()
.withUrl("./api/hub", {
accessTokenFactory: () => {
return useAppStore().token;
1 year ago
},
})
//.withAutomaticReconnect()
1 year ago
.build();
const connect = async () => {
// if (await isLogin()) {
// if (connection.state === signalR.HubConnectionState.Disconnected) {
// connection
// .start()
// .then(function () {
// console.log("signalr connected");
// })
// .catch(async function (error) {
// console.log(error);
// await isLogin();
// setTimeout(connect, 5000);
// });
// }
// }
1 year ago
};
connection.onclose(async () => {
await connect();
});
connection.on("connected", (id) => {
connectionId = id;
});
connection.on("ServerToClient", (method, data) => {
PubSub.publish(method, data);
});
export { connection, connect };