2017-05-25 167 views
1

我有一个场景,其中一个客户端向集线器方法AddMessage发送请求,而这个请求又应该将该消息广播给所有客户端,包括启动它的客户端。如何使用C#从SignalR客户端的Hub类接收广播消息?

编辑

问题是,我能够从客户端调用中心方法AddMessage如下面的代码,但我无法找到一个方法来处理客户端的广播消息这是使用以下行在Hub类中启动的。

Clients.All.NotifyMessageToClients(name, message); 

SignalR集线器类

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

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

我有关于如何通过创建一个客户端来实现的jQuery但不是在C#相同的一些想法正如我上面所做的。我将如何实现这一点,任何帮助将不胜感激。

如果上述方法没有任何意义,请给我一个正确的建议。由于

+0

我对你写这个问题的方式感到有点困惑,你不清楚你在这里问什么。你在问如何向所有客户广播一条消息?因为你已经做到了。还是你问在客户端如何接收它们?或者也许你不知道如何建立从客户端的连接?无论如何,请尽量在你的提问中尽可能明确,也许你可以告诉我们你到目前为止所尝试过的。 – Dennis1679

+0

@ Dennis1679对于混淆感到抱歉,请查看编辑部分以获得更清晰的信息。希望现在清楚 –

回答

1

你要听从这样的服务器事件:

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 StackOverflow"); 

     // 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了解更多详情。

+0

感谢您的答案。你提到过更新WPF中的UI,有没有什么方法可以更新客户端使用Web应用程序的用户界面。我会尽快将它标记为答案:) –

+0

@AjendraPrasad欢迎您。在web应用程序中,客户端代码应该是javascript以更新DOM。如果你在服务器端实现一个客户端,据我所知,它将无法更新UI。 – Alisson

+0

感谢您的意见。我现在可以通过下面的链接来完成这个工作,其中绝对URL是JavaScript中的一个引用,指向服务器。 http://www.codeguru.com/columns/experts/how-to-push-data-from-server-to-client-using-signalr.htm –

相关问题