C#C
C#3y ago
Joch

❔ SignalR

Hi,

Seeking for help. hehehe , i don't know if this correct way to implement signal in js without trigger. i used interval to call the hub.
i dont know the correct way.

sorry for my english grammar.

Thanks
"use strict";


/*
     1 sec = 1000 millisecond
*/
var MILLS = 1000; 
var SECONDS = 5;
var MILL_IN_MINS = SECONDS * MILLS

var isAlreadyShowAlert = false;
var connection = new signalR.HubConnectionBuilder()
    .withUrl("/SessionCheck")
    .withAutomaticReconnect()
    .build();

connection.on("ResponseSession", function (isLogout) {
    if (isLogout && !isAlreadyShowAlert) {
        alert("Session Forced to Logout.")
        document.getElementById("logout").click();
        isAlreadyShowAlert = true;
    }
    
});

connection.start().then(function () {
    console.log("signalR Start");
    checkSession();
}).catch(function (err) {
    console.error(err.toString());
});

function checkSession() {
    var empCode = localStorage.getItem("user.empcode")
    if (empCode !== "test") {
        setInterval(function () {
            connection.invoke("CheckSession", empCode).catch(function (err) {
                console.error(err.toString());
            });
        }, MILL_IN_MINS)
    }
}
Was this page helpful?