Search code examples
c#signalr.net-framework-version

Cannot read properties of undefined (reading 'onclose') SignalR .netFramework


I am using signalR 2.41 for creating chathub. On closing the browser close or tab close I wanto initiate disconnect from client that will remove user context details from db through Backend Here is how i am making hub at client side

 SignalrConnection = $.hubConnection(ChatUrl, {
                    useDefaultPath: false
                });
 ChatProxy = SignalrConnection.createHubProxy('CoolHub');

then i do the connect like this

function startHubConnection(companyid, callback) {
    console.log("=========== startHubConnection: attemptying to start connection");
    SignalrConnection.start().done(function () {
        console.log("connection id :" + SignalrConnection.id);
        mHubConnectionId = SignalrConnection.id;
  
        ChatProxy.invoke("Connect", companyid);
        if (callback !== undefined) {
            callback(true);
        }

    }).fail(function () {
 
        console.log(SignalrConnection);
        if (callback !== undefined) {
            callback(false);
        }
    });
}

I am trying to achieve the following but it gives me Cannot read properties of undefined (reading 'onclose')

SignalrConnection.onclose(function (event) {
    // Check if the disconnect was initiated by the user (closing tab/window).
    if (event.reason === 'browser-close') {
        // Handle the disconnect caused by closing the tab/window.
    }
});

Solution

  • I figured out Calling onDisconnected method on server side to fix my issue.