The HTTP environment itself is a connectionless architecture in which the server can only passively accept requests from clients and return results instead of actively sending data to clients. In many scenarios where real-time data interactions (such as web IM) are needed, we hope to get the data returned by the server in time. At this point, one of the most common practice is: on the client with timers, timed to request server services, to get the latest data. In this way, most of the time is doing no work, frequent requests will increase the server and the client to request the Web service consumption. So is there a better way to get the server back in time, while reducing the ability to do nothing and the performance problems of frequent requests?
Remember, not long ago, there was this article in the garden, which introduced several mechanisms for the Web environment to refresh data periodically. One of the more ingenious ways of Google Gmail is to remember how to understand it at the time and just remember the basic practice of keeping a long connection. (Of course now also can't find this article, want to know friends can remind). Today, due to the need for architectural solutions, again, consider the connection retention scheme, and refer to Gmail's request behavior, summed up, it should be: The client has maintained a connection with the server, the connection has been maintained on the server request action, until the server found that there is data after the return to it, To end the return request. After receiving the request return, the client sends a connection request to the server before processing the return, until the next time the data is returned. Inevitably, if the server does not need to send data to the client for a long time, it can cause a request failure (timeout or other reason). The same is true for this case, where a request connection is sent again in the wrong callback event. This allows you to simulate the remaining connection state.
Use pseudo code to describe the idea:
Client script:
1:function Request ()
2: {
3:ajax.request (url,onsuccessed,onfailed);
4:}
5:function onsuccessed (response)
6: {
7://Resend Request
8:request ();
9://Processing return data
10:}
11:function onfailed ()
12: {
13://error (Timeout) re-request
14:request ();
15:}
Web services:
1:public class Imservice:ihttphandler
2: {
3:public bool Isreusable{return false;
4:public void ProcessRequest (HttpContext context)
5: {
6://Read the latest data
7:while (True)
8: {
9:string message = GetMessage ();
10:if (!string. IsNullOrEmpty (message))
11: {
12:context. Response.Write (message);
13:break;
14:}
15:thread.sleep (500);//Wait a while before reading again.
16:}
17:}
18:private string GetMessage ()
19: {
20://Get the latest data
21:}
22:}
The benefits of this scheme are that the client can get the data that the server needs to send to the client at the first time (and how the Web service knows to send the data to the client, that is, the server's round robin design); You can reduce the client logic without having to create and release the timer. and reduce the resulting loss to client performance, reduce the number of requests to the server, reduce the amount of effort, save bandwidth and reduce server resources need to deal with connection requests.
It is believed that many people have been using this scheme before. You are welcome to express your views on this proposal.
Add: The server part of the design, in addition to the use of round-robin, you can also consider the use of resources mutually exclusive access to design, this can achieve better performance, higher real-time, specific scenarios should be considered in accordance with the actual situation.