如何使用 C# 在 SignalR 客户端中接收来自 Hub class 的广播消息?

How to receive broadcast message from Hub class in SignalR client using C#?

我有一个场景,其中一个客户端正在向集线器 Class 方法 AddMessage 发送请求,而集线器应该将该消息广播给所有客户端,包括发起它的客户端。

问题是我可以从客户端调用 Hub 方法 AddMessage,如下面的代码所示,但是我找不到在客户端处理广播消息的方法使用以下行在 Hub class 中启动。

Clients.All.NotifyMessageToClients(name, message);

SignalR 中心 Class

using System;
using Microsoft.AspNet.SignalR;
using System.Threading.Tasks;

public class SignalRChatHub : Hub
{
    public void AddMessage(string name, string message)
    {
        // Following call is supposed to notify all clients with passed parameters. 
        // They could have a method called NotifyMessageToClients to fetch the broadcasted message

        Clients.All.NotifyMessageToClients(name, message); 
    }
}

SignalR 客户端

using System;
using Microsoft.AspNet.SignalR.Client;

public partial class Default : System.Web.UI.Page
{
    HubConnection hubConnection;
    IHubProxy stockTickerHubProxy;

    public Default()
    {
        hubConnection = new HubConnection("http://localhost:6898/");
        stockTickerHubProxy = hubConnection.CreateHubProxy("SignalRChatHub");
    }

    async public void SendAddNotification(string msgFrom, string msg)
    {
        // Following line calls Addmessage method in SignalRChatHub class
        await stockTickerHubProxy.Invoke("Addmessage", "Ajendra", "Hello Whosebug");
    }

    // I might need the method NotifyMessageToClients here... to receive broadcasted message
}

我对如何在 jQuery 而不是 C# 中通过创建客户端实现相同的目标有一些想法,就像我在上面所做的那样。我将如何实现这一目标?

如果以上方法没有任何意义,请给我建议正确的方法。

您需要像这样监听来自服务器的事件:

public partial class Default : System.Web.UI.Page
{
    HubConnection hubConnection;
    IHubProxy stockTickerHubProxy;

    public Default()
    {
        hubConnection = new HubConnection("http://localhost:6898/");
        stockTickerHubProxy = hubConnection.CreateHubProxy("SignalRChatHub");

        // listen to server events...
        // n is "name" and m is "message", but you can change to "a" and "b" or anything else...
        stockTickerHubProxy.On<string, string>("NotifyMessageToClients", (n, m) =>
        {
            Console.WriteLine("Message received from server. Name: {0} | Message: {1}", n, m);
        });

    }

    // "async" methods should return Task instead of void....
    // unless they are event handlers for UI applications...
    public async Task SendAddNotification(string msgFrom, string msg)
    {
        // first, start the connection...
        await stockTickerHubProxy.Start();
        // Following line calls Addmessage method in SignalRChatHub class
        await stockTickerHubProxy.Invoke("Addmessage", "Ajendra", "Hello Whosebug");

        // you don't stop the connection, otherwise you won't be able to receive calls from the server
    }

}

...如果您需要在 WPF 中更新 UI,例如,您应该像这样实现您的事件:

stockTickerHubProxy.On<string, string>("NotifyMessageToClients", (a,b) => 
    Dispatcher.InvokeAsync(() =>
        {
            // update UI...
            textBox.Text += string.Format("Name: {0} | Message: {1}", a, b);
        })
);

我建议阅读 this guide 以了解更深入的细节。