简体   繁体   中英

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

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:

在此输入图像描述

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:

signalrR原型

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM