How can I close the client-side JavaScript 'Hub' connection in SignalR?

坚强是说给别人听的谎言 提交于 2021-02-10 13:16:57

问题


I have followed this tutorial.

But there is no hint how to close the Websocket connection via the HubConnection class in signalr.js-file. The file is V1.0.4.

This solution does not resolve my problem because I am using the microsofts javascript-library.

Here ist the code:

var lHubConnection = null;

var Init = function () {

    // create instance
    lHubConnection = new signalR.lHubConnectionBuilder().withUrl("/chatHub").build();

    // receive message
    lHubConnection.on("ReceiveMessage", function (pMessage) {

        // show message
        console.log(JSON.parse(pMessage));
    });

    // [...]
};

// close websocket connection
var CloseConnection = function(){

    if (lHubConnection !== null && lHubConnection.connection.connectionState === 1) {   
        // lHubConnection.invoke("?"); ???
    }
};

Here is an console output of the lHubConnection instance:


回答1:


According to Microsoft the JavaScript client contains a stop function.

https://docs.microsoft.com/en-us/javascript/api/%40aspnet/signalr/hubconnection?view=signalr-js-latest#stop

In addition, you can find the .stop()-Method in the prototype of the framework:



来源:https://stackoverflow.com/questions/52944796/how-can-i-close-the-client-side-javascript-hub-connection-in-signalr

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!