marco.pms.web/src/services/signalRService.js

38 lines
1022 B
JavaScript

import * as signalR from "@microsoft/signalr";
import { clearCacheKey, getCachedData } from "../slices/apiDataManager";
import showToast from "./toastService";
import eventBus from "./eventBus";
import { useSelector } from "react-redux";
let connection = null;
const targetPath = "";
export function startSignalR(loggedUser) {
console.log(loggedUser?.employeeInfo.id);
connection = new signalR.HubConnectionBuilder()
.withUrl("http://localhost:5032/hubs/marco", {
accessTokenFactory: () => localStorage.getItem("jwtToken"),
withCredentials: false,
})
.withAutomaticReconnect()
.build();
connection.on("Attendance", (data) => {
clearCacheKey("Attendance");
if (data.loginUserId != loggedUser?.employeeInfo.id) {
eventBus.emit('attendance', data );
}
});
connection
.start()
.then(() => console.log("SignalR connected"))
.catch((err) => console.error("SignalR error:", err));
}
export function stopSignalR() {
if (connection) connection.stop();
}