问题
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