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.
43 lines
1.0 KiB
43 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: () => {
|
||
|
const appStore = useAppStore();
|
||
|
return appStore.token;
|
||
|
},
|
||
|
})
|
||
|
.withAutomaticReconnect()
|
||
|
.build();
|
||
|
const connect = async () => {
|
||
|
return;
|
||
|
if (await isLogin()) {
|
||
|
if (connection.state === signalR.HubConnectionState.Disconnected) {
|
||
|
connection
|
||
|
.start()
|
||
|
.then(function () {
|
||
|
console.log("signalr connected");
|
||
|
})
|
||
|
.catch(function (error) {
|
||
|
console.error(error);
|
||
|
//setTimeout(connect, 5000);
|
||
|
});
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
connection.onclose(async () => {
|
||
|
await connect();
|
||
|
});
|
||
|
connection.on("connected", (id) => {
|
||
|
connectionId = id;
|
||
|
});
|
||
|
connection.on("ServerToClient", (method, data) => {
|
||
|
PubSub.publish(method, data);
|
||
|
});
|
||
|
|
||
|
export { connection, connect };
|