【问题标题】:Using ASP.NET Core SignalR in .NET Console Client App to Receive Messages from Azure SignalR在 .NET 控制台客户端应用程序中使用 ASP.NET Core SignalR 从 Azure SignalR 接收消息
【发布时间】:2019-04-23 15:22:19
【问题描述】:

我正在学习 SignalR,但遇到了障碍。

我有一个 Azure 函数已成功发布到 Azure SignalR 托管服务(在无服务器模式下配置)

我一直在关注这个快速入门:

Quickstart: Create a chat room with Azure Functions and SignalR Service using C#

我想要实现的基本上是将来自服务器的消息接收到我的客户端应用程序中。为了对此进行原型设计,我创建了一个控制台应用程序。

我添加了以下 Nuget 包

Microsoft.AspNetCore.SignalR.Client -版本 1.1.0 Microsoft.Azure.WebJobs.Extensions.SignalRService

所有基础架构似乎都运行良好 - 我假设我可以在以下地址运行演示网站,将其指向我的本地实例(或托管在 Azure 中的实例) https://azure-samples.github.io/signalr-service-quickstart-serverless-chat/demo/chat-v2/

我的 AzureFunction 发布的消息直接发布到聊天窗口中。

如何将这些消息打印到控制台?

using Microsoft.AspNetCore.SignalR.Client;
using Microsoft.Azure.WebJobs.Extensions.SignalRService;
using System;
using System.Threading.Tasks;

namespace ConsoleApp2
{
    class Program
    {
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            Console.ReadKey();

            var connection = new HubConnectionBuilder().WithUrl("http://localhost:7071/api").Build();


            connection.On<SignalRMessage>("newMessage", (message) =>
            {
                Console.WriteLine(message.Arguments);
            });


            connection.On("newMessage", (string server, string message) =>
            {
                Console.WriteLine($"Message from server {server}: {message}");
            }
  );
            await connection.StartAsync();
            Console.ReadKey();
        }
    }
}

我强烈怀疑我的问题与

有关

connection.On<...>

声明。他们从不开火。 Connection.StartAsync() 似乎工作正常,并建立了与 Azure SignalR 实例的连接。

我是否遗漏了一些基本观点?我只是在这一点上挣扎。

简而言之 - 谁能给我一个指向接收和写入消息到我的控制台窗口的指针 - 与在网络聊天演示中将消息打印到网络浏览器的方式非常相似(参见上面的第二个链接)。

这些消息是简单的广播消息,我想发送给所有连接的客户端。

几乎所有示例都使用 Javascript。

提前致谢。

【问题讨论】:

    标签: c# azure signalr.client asp.net-core-signalr azure-signalr


    【解决方案1】:

    一旦我发现如何向 SignalR 添加日志记录,我可以看到它无法解析正在发送的类型。

    一旦我改变了我的连接,它就起作用了。在正确的类型上,比如

    connection.On<CorrectType>("newMessage", (message) =>
                {
                    Console.WriteLine(message.stringproperty);
                });
    

    看文章Azure Functions development and configuration with Azure SignalR Service我的想法被误导了

    他们“看似”(至少在我看来)向 SignalR 添加了“SignalRMessage”类型的消息。事实上,他们正在添加“CorrectType”的消息类型

    CorrectType message
    signalRMessages.AddAsync(
        new SignalRMessage
        {
            // the message will only be sent to these user IDs
            UserId = "userId1",
            Target = "newMessage",
            Arguments = new [] { message }
        });
    

    【讨论】:

      【解决方案2】:

      我设法通过在调用 'connection.On' 时传入 'object' 来绕过它,而不必创建 CorrectType 类并让 .net 弄清楚对象的外观。

      确实是类型的解析阻止了 .On 在 Windows 客户端上触发。

      【讨论】:

        猜你喜欢
        • 2017-04-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-09-27
        • 1970-01-01
        • 2016-04-04
        • 2022-09-24
        • 2019-08-26
        相关资源
        最近更新 更多