this.signalr = new signalR.HubConnectionBuilder()
.withUrl('/pipelinehub')
.build();
this.signalr.start();
this.signalr.on('somemethod', function () { ... });
那么,如果我想停止听这种方法,我该怎么办?
根据 SignalR 源代码,您可以使用off
:
从服务器中心删除 给定事件名称
off: function (eventName, callback, callbackIdentity) {
/// <summary>Removes the callback invocation request from the server hub for the given event name.</summary>
/// <param name="eventName" type="String">The name of the hub event to unregister the callback for.</param>
/// <param name="callback" type="Function">The callback to be removed.</param>
但是,当您同时使用On
和Off
时,您需要小心。看看:
jQuery SignalR 客户端 .off(( 函数仅删除上次注册的回调