SignalR组可以在不同的集线器之间共享吗?

在找到与组通信的this update后,似乎一个组被锁定了集线器名称 . 如果这是正确的(请告诉我,如果我还是有更好的团队或更好的全球团队?

我的问题是我有一个集线器将客户端添加到一个组:

public class GameService : Hub, IGameService
...
public void CreateNewGame(CreateGameDto game)
    {
        game.Creator = UserRepo.GetUser(Context.ConnectionId);
        var newGame = GameRepo.CreateNewGame(game);
        Groups.Add(Context.ConnectionId, newGame.GameId.ToString(CultureInfo.InvariantCulture));
        Clients.Caller.JoinedGame(newGame);
    }

另一个中心完全需要向该组广播:

public class MessagingService : Hub , IMessageService
    ...
        public void AddMessage(MessageDto message)
    {
        message.User = UserRepo.GetUser(Context.ConnectionId);
        MessageRepo.AddMessage(message);
        Clients.Group(message.User.GameId.ToString(CultureInfo.InvariantCulture))
            .ReceivedMessage(message);
    }

截至目前,客户端从未收到此广播 .

Edited to add the client code. 这是客户端信号器设置,我've created a framework that allows me to use the service interface with a Castle Dynamic Proxy so that I'm没有按字符串名称调用服务和方法 .

public abstract class BaseClientProxy<TServer,TClient>
{
    public  TServer ServiceProxy;

    protected BaseClientProxy(HubConnection conn)
    {
        ServiceProxy = CreateProxy(conn);
    }

    private ProxyGenerator _generator;
    protected IHubProxy Proxy;
    protected TClient Receiver;

    protected TServer CreateProxy(HubConnection conn)
    {
        Proxy = conn.CreateHubProxy<TServer>();

        _generator = new ProxyGenerator();

        return (TServer)_generator.CreateInterfaceProxyWithoutTarget(typeof(TServer), new HubProxyInterceptor(Proxy));
    }

    public void SetReceiver(TClient receiver)
    {
        Receiver = (TClient)_generator.CreateInterfaceProxyWithTarget(typeof(TClient), receiver);
        RegisterEvents();
    }

    protected void RegisterEvents()
    {
        Action<MethodInfo> regAction = RegisterEvent<object>;
        var methods =
            typeof (TClient).GetMethods(BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public);
        foreach (var methodInfo in methods)
        {
            var locInfo = methodInfo;
            if (locInfo.GetParameters().Length > 0)
            {
                var regMethod = typeof(BaseClientProxy<TServer, TClient>).GetMethodExt(regAction.Method.Name, typeof(MethodInfo));
                var genAction = regMethod.MakeGenericMethod(locInfo.GetParameters()[0].ParameterType);
                genAction.Invoke(null, new object[] { locInfo });
            }
            else
            {
                Proxy.On(locInfo.Name, () =>locInfo.Invoke(Receiver, null));
            }
        }
    }

    protected void RegisterEvent<TDto>(MethodInfo method)
    {
        Proxy.On<TDto>(method.Name, x => method.Invoke(Receiver, new object[] {x}));
    }
}

public class HubProxyInterceptor : IInterceptor
{
    protected IHubProxy Proxy;

    public HubProxyInterceptor(IHubProxy proxy) 
    {
        Proxy = proxy;
    }

    protected async void Invoke<TDto>(string methodName, TDto dto)
    {
        await Proxy.Invoke(methodName, dto);
    }

    protected async void Invoke(string methodName)
    {
        await Proxy.Invoke(methodName);
    }

    public void Intercept(IInvocation invocation)
    {
        if (invocation.Arguments.Length > 0)
        {
            Invoke(invocation.Method.Name, invocation.Arguments[0]);
        }
        else
        {
            Invoke(invocation.Method.Name);
        }
    }
}

这些片段来自单个类,它 Build 连接并在windsor容器中注册服务 .

private SignalRManager()
    {
        var settings = Settings.GetSettings<IClientSettings>(ConfigurationManager.AppSettings);

        Connection = new HubConnection(settings.SignalRServerUri);

        ioc = new WindsorContainer();
        ioc.Register(
            Component.For<BaseClientProxy<IUserService, IUserClient>>().Instance(new UserServiceProxy(Connection)),
            Component.For<BaseClientProxy<IDrawingService, IDrawingClient>>().Instance(new DrawingServiceProxy(Connection)),
            Component.For<BaseClientProxy<IMessageService, IMessageClient>>().Instance(new MessageServiceProxy(Connection)),
            Component.For<BaseClientProxy<IScoreBoardService, IScoreBoardClient>>().Instance(new ScoreBoardServiceProxy(Connection)),
            Component.For< BaseClientProxy<IGameService,IGameClient>>().Instance(new GameServiceProxy(Connection)));
        Connection.Start().Wait();
    }
   public TClient GetService<TClient,TReceiver>(TReceiver receiver) where TClient : IService
    {
        var proxy = ioc.Resolve<BaseClientProxy<TClient, TReceiver>>();
        proxy.SetReceiver(receiver);
        return proxy.ServiceProxy;
    }

在此代码中,HubProxyInterceptor负责对服务器进行所有调用 .

BaseClientProxy中的RegisterEvents方法负责将从服务器到客户端的调用连接起来 . TServer和TClient类型参数是2个不同但相似的接口,TServer将由服务器端的Hub类实现,TClient由我的WPF视图模型实现,该模型是在调用SetReceiver时传递的 .

回答(1)

2 years ago

您可以在MessagingService.AddMessage中使用GetHubContext将消息发送到另一个Hub的组 .

public void AddMessage(MessageDto message)
{
    IHubContext gameContext = GlobalHost.ConnectionManager.GetHubContext<GameService>();

    message.User = UserRepo.GetUser(Context.ConnectionId);
    MessageRepo.AddMessage(message);

    gameContext.Clients.Group(message.User.GameId.ToString(CultureInfo.InvariantCulture))
        .ReceivedMessage(message);
}

如果您不想在每次调用AddMessage时重新创建 gameContext ,则可以将其存储在字段中 .