Push notification from an ASP.Net server

2019-02-19 13:28发布

问题:

I have a web service that performs some operations. When an event occurs I will like to notify the clients. The problem that I have is that I am able to connect from the client to the server but not the other way around because clients happen to be behind a NAT (router). Currently I am making a request every minute to check for notifications. It would be nice if I can have a technique where I could notify the clients faster without having to make so many unnecessary request.

Note: The client is a c# console application and the server is a asp.net website.

(note if an event happens on the server I will like to notify all clients)

回答1:

Use SignalR. This is Microsoft's new library for abstracting out real time server-client connections and if your setup allows it, it supports the new WebSockets protocol.

Taken from asp.net website. The Client Code

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));
await hubConnection.Start();