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

时间:2014-02-12 23:37:08

标签: 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);
    }

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

已修改为添加客户端代码。 这是客户端信号器设置,我创建了一个框架,允许我使用带有Castle Dynamic Proxy的服务接口,这样我就不会按字符串名称调用服务和方法。

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);
        }
    }
}

这些片段来自单个类,它建立连接并在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 个答案:

答案 0 :(得分:5)

您可以在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,都可以将{{1}}存储在字段中。