简体   繁体   中英

Signalr with a desktop based applications in real time

I always knew SignalR as fitting perfectly with browser based applications in real time.

In general to pushing server side processing messages to the client, that is a "listening" web page.

It is possible to do the same with client that is not a web application, but a desktop application, pushing it in real-time?

In short, yes. The samples on github show you how, for instance the console client sample , and the documentation in the wiki shows you how you can build a .NET client . To quote that documentation (warning, version dependent, it works right now, but may be different in the future):

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();

See ASP.NET: ASP.NET SignalR Hubs API Guide for the above code.

I have made a wrapper around the .NET client that makes it really easy to implement listeners on the local client

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

Once set up you just add a listener like

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM