如何使SignalR向所有具有不同主机的客户端发送通知

问题描述 投票:0回答:1

我有一个包含Web应用程序和API应用程序的系统。这两个应用都使用相同的服务层。 (这是3级架构)。我在Service层上组织SignalR并在Web App的Startup.cs文件中注册它。

当我调用SendTo Hub()方法时,SignalR在Web应用程序上正常工作。但是如果我通过API调用SendTo Hub(),则没有任何反应。

我期望WebApp的Front-end应该在从API App引发SignalR时收到通知。

来自服务的电话

public void SendSignal() {
     hub_communication.SendToHub(clientId:"", incommingText:"")
}

从Web应用和API调用:

Service.SendSignal();

一些SignalR配置:

public class MainHub : Hub {
        public void Register(string group)
        {
            var clients = GlobalHost.ConnectionManager.GetHubContext<MainHub>();            
            UnRegisterAll(group, null);
            SignalRConnectionToGroupsMap.TryAddGroup(Context.ConnectionId, group);
            Clients.Caller.groupAdded(group);
        }
        public void UnRegisterAll(string userID, string organizationID)
        {
            var rets = new List<string>();
            SignalRConnectionToGroupsMap.TryRemoveAll(Context.ConnectionId, out rets);
            var clients = GlobalHost.ConnectionManager.GetHubContext<MainHub>();
            Clients.Caller.groupRemovedAll(userID);
        }
}

public static class hub_communication
    {
        public static void SendToHub(string clientid, string incoming)
        {
            var clients = GlobalHost.ConnectionManager.GetHubContext<MainHub>();
            clients.Clients.Group(clientid).receiveMessage(incoming);
        }        
    }
public static class SignalRConnectionToGroupsMap
    {
        private static readonly ConcurrentDictionary<string, List<string>> Map = new ConcurrentDictionary<string, List<string>>();

        public static bool TryAddGroup(string connectionId, string groupName)
        {
            if (!Map.TryGetValue(connectionId, out var groups))
            {
                return Map.TryAdd(connectionId, new List<string>() { groupName });
            }

            if (!groups.Contains(groupName))
            {
                groups.Add(groupName);
            }

            return true;
        }

        public static bool TryRemoveAll(string connectionId, out List<string> result)
        {
            return Map.TryRemove(connectionId, out result);
        }
    }

Startup.cs

public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ...
            app.Map("/signalr", map =>
            {                
                map.UseCors(CorsOptions.AllowAll);
                var hubConfiguration = new HubConfiguration();                
                map.RunSignalR(hubConfiguration);
            });
        }
}
c# signalr signalr-hub signalr.client
1个回答
0
投票

这里的问题是您的应用程序在不同的域上运行。在Startup.cs文件中。如下配置SignalR:

        app.Map("/signalr", map =>
        {
            // Setup the CORS middleware to run before SignalR.
            // By default this will allow all origins. You can 
            // configure the set of origins and/or http verbs by
            // providing a cors options with a different policy.
            map.UseCors(CorsOptions.AllowAll);
            var hubConfiguration = new HubConfiguration 
            {
                // You can enable JSONP by uncommenting line below.
                // JSONP requests are insecure but some older browsers (and some
                // versions of IE) require JSONP to work cross domain
                // EnableJSONP = true
            };
            // Run the SignalR pipeline. We're not using MapSignalR
            // since this branch already runs under the "/signalr"
            // path.
            map.RunSignalR(hubConfiguration);
        });
© www.soinside.com 2019 - 2024. All rights reserved.