【问题标题】:Signalr with a desktop based applications in real timeSignalr 具有基于桌面的实时应用程序
【发布时间】:2014-02-12 11:57:52
【问题描述】:

我一直都知道SignalR 非常适合实时基于浏览器的应用程序。

一般来说,将服务器端处理消息推送到客户端,即“监听”网页。

是否可以对不是 Web 应用程序而是桌面应用程序的客户端执行相同的操作,实时推送它?

【问题讨论】:

  • 在问这里之前你有没有试过搜索?
  • 是的,我试过了,但我没有运气

标签: c# signalr desktop-application


【解决方案1】:

简而言之,是的。 github 上的示例向您展示了如何使用,例如 console client sample,而 wiki 中的文档向您展示了如何使用 build a .NET client。引用该文档(警告,取决于版本,它现在可以工作,但将来可能会有所不同):

var hubConnection = new HubConnection("http://www.contoso.com/");
IHubProxy stockTickerHubProxy = hubConnection.CreateHubProxy("StockTickerHub");
stockTickerHubProxy.On<Stock>("UpdateStockPrice", stock => Console.WriteLine("Stock update for {0} new price {1}", stock.Symbol, stock.Price));

stockTickerHub.On("notify", () =>
    // Context is a reference to SynchronizationContext.Current
    Context.Post(delegate
    {
        textBox.Text += "Notified!\n";
    }, null)
);    

await hubConnection.Start();
// or do the following for a synchronous method:
// connection.Start().Wait();

以上代码见ASP.NET: ASP.NET SignalR Hubs API Guide

【讨论】:

    【解决方案2】:

    我对 .NET 客户端做了一个包装器,这使得在本地客户端上实现监听器变得非常容易

    https://github.com/AndersMalmgren/SignalR.EventAggregatorProxy/wiki/.NET-Client

    设置完成后,您只需添加一个监听器,例如

    public class MyViewModel : IHandle<MyEvent>
    {
       public MyViewModel(IEventAggregator eventAggregator) 
       {
          eventAggregator.Subscribe(this);
       }
       public void Handle(MyEvent message)
       {
          //Act on MyEvent
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-10-06
      • 2015-09-24
      • 1970-01-01
      • 2010-09-30
      • 1970-01-01
      • 1970-01-01
      • 2011-12-01
      • 2015-11-20
      相关资源
      最近更新 更多