Detailed .NET Core SignalR Redis base (II)

Connect the text.

Speaking Clients.All.SendAsync above method is actually called SendCoreAsync of AllClientProxy. Actually, the key is to call IClientProxy of SendCoreAsync. In HubClients class comes in above. There are a lot of IClientProxy the implementation class. All such just said is actually AllClientProxy object.

public IClientProxy AllExcept(IReadOnlyList<string> excludedConnectionIds)
        {
            return new AllClientsExceptProxy<THub>(_lifetimeManager, excludedConnectionIds);
        }

        public IClientProxy Client(string connectionId)
        {
            return new SingleClientProxy<THub>(_lifetimeManager, connectionId);
        }

        public IClientProxy Group(string groupName)
        {
            return new GroupProxy<THub>(_lifetimeManager, groupName);
        }

        public IClientProxy GroupExcept(string groupName, IReadOnlyList<string> excludedConnectionIds)
        {
            return new GroupExceptProxy<THub>(_lifetimeManager, groupName, excludedConnectionIds);
        }

        public IClientProxy Clients(IReadOnlyList<string> connectionIds)
        {
            return new MultipleClientProxy<THub>(_lifetimeManager, connectionIds);
        }

        public IClientProxy Groups(IReadOnlyList<string> groupNames)
        {
            return new MultipleGroupProxy<THub>(_lifetimeManager, groupNames);
        }

        public IClientProxy User(string userId)
        {
            return new UserProxy<THub>(_lifetimeManager, userId);
        }

        public IClientProxy Users(IReadOnlyList<string> userIds)
        {
            return new MultipleUserProxy<THub>(_lifetimeManager, userIds);
        }

  These implementation classes there are in fact the constructor and SendCoreAsync stereotyped. What matters is HubLifetimeManager object. Above it said. In AdRedis when he has put RedisHubLifetimeManager injected into it. So here is actually true calling or SendAllAsync method RedisHubLifetimeManager in.

public override Task SendAllAsync(string methodName, object[] args, CancellationToken cancellationToken = default)
        {
            var message = _protocol.WriteInvocation(methodName, args);
            return PublishAsync(_channels.All, message);
        }

  WriteInvocation finished sending broadcast in the process. (This code is too obscure. I did not understand, just a ballpark guess). Redis is implemented in PublishAsync push.

Then the other server is how to receive broadcast it?

Also in RedisHubLifetimeManager in. Rewrite OnConnectedAsync method.

public override async Task OnConnectedAsync(HubConnectionContext connection)
        {
            await EnsureRedisServerConnection();
            var feature = new RedisFeature();
            connection.Features.Set<IRedisFeature>(feature);

            var connectionTask = Task.CompletedTask;
            var userTask = Task.CompletedTask;

            _connections.Add(connection);

            connectionTask = SubscribeToConnection(connection);

            if (!string.IsNullOrEmpty(connection.UserIdentifier))
            {
                userTask = SubscribeToUser(connection);
            }

            await Task.WhenAll(connectionTask, userTask);
        }

  Subscribe here Redis completed by SubscribeToConnection method. This method is called HubConnectionHandler OnConnectedAsync in OnConnectedAsync in. It can be seen from the name of HubConnectionHandler deal HubConnection. The call HubConnectionHandler is when we are at the beginning of UseHub.

public static IConnectionBuilder UseHub<THub>(this IConnectionBuilder connectionBuilder) where THub : Hub
        {
            var marker = connectionBuilder.ApplicationServices.GetService(typeof(SignalRCoreMarkerService));
            if (marker == null)
            {
                throw new InvalidOperationException("Unable to find the required services. Please add all the required services by calling " +
                    "'IServiceCollection.AddSignalR' inside the call to 'ConfigureServices(...)' in the application startup code.");
            }

            return connectionBuilder.UseConnectionHandler<HubConnectionHandler<THub>>();
        }

  So far. To explain the entire broadcast process is completed.

Why not here to talk about the method of the Preamble. Because in fact exposed by the interface is to take the form of Http protocol. The Redis subscription agreement and the release is to take the RESP. Http Hypertext Transfer Protocol under sub-room and more cases will use up a lot of bandwidth IIS extra processing resources. And with subscriptions to the publication mode Redis, and other ways of thinking Channel way Signalr the Group, etc. In fact, the same.

Last posted about Microsoft's official description.

Set ASP.NET Core SignalR scale plate Redis

https://docs.microsoft.com/zh-cn/aspnet/core/signalr/redis-backplane?view=aspnetcore-2.2

Tutorial: Getting Started with ASP.NET Core SignalR

https://docs.microsoft.com/zh-cn/aspnet/core/tutorials/signalr?view=aspnetcore-2.2&tabs=visual-studio

Guess you like

Origin www.cnblogs.com/dbdn/p/11390928.html