SignalR 一次订阅所有中心/方法

SignalR subscribe all hubs/methods at once

本文关键字:方法 一次 SignalR      更新时间:2023-09-26

我想在客户端为 SignalR 制作一个简单的包装器。它基本上应该具有方法send(hubName, methodName, args)并触发某种callback(hubName, methodName, args),而无需显式订阅特定中心。

有没有简单的方法可以做到这一点,或者我应该深入研究jquery.signalR?提前谢谢。

所以它似乎是这样的:

function send(hub, method, args) {
    $.signalR.hub.send({
        H: method,
        M: method,
        A: args,
        I: requestId++ // some global var which increments with every request
    });
}
$.signalR.hub.received(function(resp) {
    if (resp.I) {
        const requestId = resp.I;
        const response = resp.R;
        console.log('Response received', requestId, response);
    } else {
        const hub = resp.H;
        const method = resp.M;
        const args = resp.A;
        console.log('Server sent event received', hub, method, args);
    }
});

I是一种请求计数器。它包含在相应请求的响应中。