【问题标题】:Multiple SignalR connections in ABPABP 中的多个 SignalR 连接
【发布时间】:2018-02-21 03:25:48
【问题描述】:

我有一个使用 ASP.NET 零模板的项目。我已成功将 SignalR 集成到我的解决方案中,并且实时通知工作正常。我想要的是在我的解决方案中添加另一个集线器或扩展现有的 SignalR 集线器以添加更多内容。

SignalR AspNetCore Integration 文档中,它说将以下内容添加到 Startup.cs 文件中:

app.UseSignalR(routes =>
{
     routes.MapHub<AbpCommonHub>("/signalr"); // default hub
     routes.MapHub<HitchNotification.HitchHub>("/hitchHub"); // my hub
});

然而,问题是我需要在客户端建立连接!在 SignalRAspNetCoreHelper.ts 中,它将 URL 设置为使用 '/signalr' 集线器(默认之一)。

export class SignalRAspNetCoreHelper {
    static initSignalR(): void {

        var encryptedAuthToken = new UtilsService().getCookieValue(AppConsts.authorization.encrptedAuthTokenName);

        abp.signalr = {
            autoConnect: true,
            connect: undefined,
            hubs: undefined,
            qs: AppConsts.authorization.encrptedAuthTokenName + "=" + encodeURIComponent(encryptedAuthToken),
            url: AppConsts.remoteServiceBaseUrl + '/signalr'
        };

        jQuery.getScript(AppConsts.appBaseUrl + '/assets/abp/abp.signalr-client.js');
    }
}

如果我将'/signalr' 更改为'/hitchHub',它可以正常工作。但我想要两者都在我的应用程序中!我尝试为我自己的集线器创建一个类似于 SignalRAspNetCoreHelper.ts 的助手,并在 app.component.ts 中对其进行初始化:

ngOnInit(): void {
    if (this.appSession.application && this.appSession.application.features['SignalR']) {
        if (this.appSession.application.features['SignalR.AspNetCore']) {
            SignalRAspNetCoreHelper.initSignalR();
            HitchHubHelper.initHitchHub();  
        } 
    }
}

但似乎abp.signalr 不能有多个连接到不同的集线器。

所以,基本上我有两个问题:

  1. 有什么方法可以将我自己的集线器功能添加到默认的AbpCommonHub 中?这样,我可以简单地修改 abp.signalr-client.js 文件。

  2. 如果上述情况不可行,我如何在abp.signalr 上拥有多个集线器以便在我的应用程序中的任何位置都可以访问?

【问题讨论】:

    标签: angular typescript asp.net-core signalr aspnetboilerplate


    【解决方案1】:
    1. 有什么方法可以将我自己的集线器功能添加到现有的默认 AbpCommonHub 中?这样我就可以简单地修改 abp.signalr-client 文件

    当然。继承AbpCommonHub:

    public class HitchHub: AbpCommonHub
    {
        // Ctor omitted for brevity
    
        public async Task SendMessage(string message)
        {
            await Clients.All.SendAsync("getMessage", string.Format("User {0}: {1}", AbpSession.UserId, message));
        }
    }
    

    更换集线器:

    // routes.MapHub<AbpCommonHub>("/signalr");
    routes.MapHub<HitchHub>("/signalr");
    
    1. 如果上述情况不可行,如何在 abp.signalr 上设置多个集线器以便在我的应用程序中的任何位置都可以访问?

    以上并非不可能,但无论如何我都会回答这个问题以演示多个集线器(对于 Angular)。

    继承AbpHubBase:

    public class HitchHub : AbpHubBase, ITransientDependency
    {
        public async Task SendMessage(string message)
        {
            await Clients.All.SendAsync("getMessage", string.Format("User {0}: {1}", AbpSession.UserId, message));
        }
    }
    

    地图中心:

    routes.MapHub<AbpCommonHub>("/signalr"); // No change
    routes.MapHub<HitchHub>("/signalr-hitchHub"); // Prefix with '/signalr'
    

    用法

    这需要Abp.AspNetCore.SignalR v3.5.0-preview3

    修改SignalRAspNetCoreHelper.ts

    abp.signalr = {
        autoConnect: true,  // No change
        connect: undefined, // No change
        hubs: undefined,    // No change
        qs: AppConsts.authorization.encrptedAuthTokenName + "=" + ... // No change
        remoteServiceBaseUrl: AppConsts.remoteServiceBaseUrl,         // Add this
        startConnection: undefined,                                   // Add this
        url: '/signalr' // Changed from: AppConsts.remoteServiceBaseUrl + '/signalr'
    };
    
    // Modify the following block
    jQuery.getScript(AppConsts.appBaseUrl + '/assets/abp/abp.signalr-client.js', () => {
        var hitchHub;
    
        abp.signalr.startConnection('/signalr-hitchHub', function (connection) {
            hitchHub = connection; // Save a reference to the hub
    
            connection.on('getMessage', function (message) { // Register for incoming messages
                console.log('received message: ' + message);
            });
        }).then(function (connection) {
            abp.log.debug('Connected to hitchHub server!');
            abp.event.trigger('hitchHub.connected');
        });
    
        abp.event.on('hitchHub.connected', function() { // Register for connect event
            hitchHub.invoke('sendMessage', "Hi everybody, I'm connected to the chat!"); // Send a message to the server
        });
    });
    

    【讨论】:

    • 感谢 Aaron,我最终选择了从 AbpCommonHub 继承的第一个选项。我的 SendMessage 方法工作正常。但由于某种原因,实时通知已停止工作!我没有收到任何错误!有什么意见吗?
    • 你还修改了什么?
    • 没什么!我期待 connection.on('getNotification', function (notification) { abp.event.trigger('abp.notifications.received', notification); });像以前一样工作,但自从更改后它就停止工作了。
    • 哦,你需要替换IRealTimeNotifier,因为this
    • Aaron,如前所述,实时通知在上述更改之前工作,即使现在我将代码恢复到将开始工作的初始状态。我最近更新了所有的 DLL,我相信我的项目已经有了 IRealTemeNotifier 的修改版本。
    猜你喜欢
    • 2015-06-03
    • 2020-01-06
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多