【问题标题】:SignalR Core with Redis Pub\Sub and console applicationSignalR Core 与 Redis Pub\Sub 和控制台应用程序
【发布时间】:2018-06-27 08:27:07
【问题描述】:

我正在使用带有 SignalR Core 1.0.1 的 Asp.Net Core 2.1。

我创建了此处描述的聊天应用程序: https://docs.microsoft.com/en-us/aspnet/core/tutorials/signalr?view=aspnetcore-2.1&tabs=visual-studio

还配置了 SignalR 以使用 Redis

services.AddSignalR().AddRedis(Configuration["ConnectionStrings:Redis"]);

使用 redis-cli monitor 运行 Redis 服务器后,我可以看到以下命令:

1530086417.413730 [0 127.0.0.1:57436] "订阅" "SignalRCore.Hubs.ChatHub:connection:VAIbFqtNyPVaod18jmm_Aw"

1530086428.181854 [0 127.0.0.1:57435] "PUBLISH" "SignalRCore.Hubs.ChatHub:all" "\x92\x90\x81\xa4json\xc4W{\"type\":1,\"target\": \"ReceiveMessage\",\"arguments\":[{\"user\":\"user\",\"message\":\"message\"}]}\x1e"

一切正常,直到我想从另一个控制台应用程序推送一些消息。 在该应用程序中,我使用的是 ServiceStack.Redis,代码如下:

var redisManager = new RedisManagerPool(configuration["ConnectionStrings:Redis"]);
using (var client = redisManager.GetClient())
{
    client.PublishMessage("SignalRCore.Hubs.ChatHub:all", "{\"type\":1,\"target\":\"ReceiveMessage\",\"arguments\":[{\"user\":\"FromConsole\",\"message\":\"Message\"}]");
}

浏览器不处理消息。我认为情况是在用于 SignalR 的附加信息中:

"\x92\x90\x81\xa4json\xc4W{...}\x1e"

相关监控记录:

1530087843.512083 [0 127.0.0.1:49480] "PUBLISH" "SignalRCore.Hubs.ChatHub:all" "{\"type\":1,\"target\":\"ReceiveMessage\",\"arguments\ ":[{\"user\":\"FromConsole\",\"message\":\"Message\"}]"

任何想法如何指定此附加数据以进行发布? 可能我应该使用更适合我的情况的东西而不是 ServiceStack.Redis

【问题讨论】:

    标签: servicestack.redis asp.net-core-signalr


    【解决方案1】:
    using Microsoft.AspNetCore.SignalR.Protocol;
    
    using Microsoft.AspNetCore.SignalR.Redis.Internal;
    
    using StackExchange.Redis;
    
    using System.Collections.Generic;
    
        static void Main(string[] args)
        {
            using (var redis = ConnectionMultiplexer.Connect("127.0.0.1:6379"))
            {
                var sub = redis.GetSubscriber();
    
                var protocol = new JsonHubProtocol();
                var redisProtocol = new RedisProtocol(new List<JsonHubProtocol>() { protocol});
                var bytes = redisProtocol.WriteInvocation("ReceiveMessage", new[] { "60344", "60344" });
    
                sub.Publish("SignalRChat.Hubs.ChatHub:all", bytes);
            }
        }
    

    如何找到它?

    1. 在signalr源码中搜索“.Publish”,可以找到https://github.com/aspnet/SignalR/blob/c852bdcc332ffb998ec6a5b226e35d5e74d24009/src/Microsoft.AspNetCore.SignalR.StackExchangeRedis/RedisHubLifetimeManager.cs

    2. 它使用 RedisProtocol 和消息包来 .WriteBytes。页眉页脚名称计数...

    【讨论】:

    • 您也可以在 Microsoft.AspNetCore.SignalR.Redis.Internal { internal class RedisChannels 中看到 Channels
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 2012-08-12
    • 2012-06-23
    • 1970-01-01
    相关资源
    最近更新 更多