The client (desktop app) pulls data…but I want the

2019-03-16 10:31发布

问题:

I have a client-server application - wherein the server is essentially an ASP .NET web application and the distributed clients are desktop applications.

The clients need to receive some data from the server - when there is new data for the client. Right now, the way this is done is - the client keeps querying a web service every x minutes (say 2 minutes) and keeps checking if there's new data for the client.

Ideally, the way it should work is that the desktop app should receive updates as and when they are available, it need not pull from the server; instead the server should be able to push to the client.

How do I go about doing this - given the architecture of the solution - a web application needs to push data to desktop applications (clients) in the same network (a LAN)?

回答1:

What you're describing is "server push", which these days is often called "COMET". Using those keywords in a web search should turn up a lot of useful information.

The most common technique for this is called a "hanging GET". The client sends a GET request to a specific URL, and the server accepts the connection but delays sending a response until it has data to send. When the client receives the response it sends another GET so it's ready for another message.



回答2:

You could use WCF callbacks - this are a web service where you can subscribe to notifications from a client and the server will send messages to subscribed clients. I have a beginners guide on my blog.



回答3:

You may be interested in the SO question. What you are describing sounds like a Comet application - server push to a client.



回答4:

Check out WebSync; it's a Comet solution for ASP.NET/IIS, but there's also a full .NET client available, which enables integration with thick clients, windows services, etc. So it sounds like it should fit the bill pretty nicely.



回答5:

If you can leave a socket open, the client can connect to the server and the server can just push data down the socket when appropriate. There is no reason why the side that initiates the connection must always be the one to initiate the data transfer.