Can I use the SignalR group between different hubs?

After finding this update in communication with groups, it seems that the group is disconnected on behalf of the hub. If this is correct (let me know if I am wrong) is there any way for the hub to access another group of hubs, or better yet, to have some kind of global group?

My problem is that I have one hub that adds the client to the group:

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

and the other hub should be fully translated into this group:

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

As of now, the client never receives this broadcast.

. signalr , , - Castle, .

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

, .

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 .

RegisterEvents BaseClientProxy . TServer TClient 2 , , TServer Hub , TClient WPF, , SetReceiver.

+4
1

GetHubContext MessagingService.AddMessage -.

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

gameContext , AddMessage.

+4

Source: https://habr.com/ru/post/1526644/


All Articles