当前位置:网站首页>Polling and long polling

Polling and long polling

2022-06-25 12:28:00 Yu Qin

polling : To put it bluntly, the client requests the server regularly ,   It is the client's active request to promote data update ;

Long polling : To put it bluntly, it is also the client request server , But the server does not return immediately , Instead, the content is returned to the client only when it is updated , In terms of process , It can be understood that the server pushes content to the client ;

    

You can see the difference :

polling :

  1: It consumes a lot of server memory and broadband resources , Because the server keeps requesting , A lot of times No new data updates , Therefore, most requests are invalid requests

  2: Data is not necessarily updated in real time , It depends on the set request interval , Basically there will be delays .

Long polling :

  1: Solved two major problems of polling , Real time data update ;

  2: The only disadvantage is that the server consumes more memory when it is suspended ;

web In communication A long connection Long polling

be based on HTTP Long connection of , It's a long polling method “ Server push ” Technology , It makes up for HTTP The disadvantages of simple request response mode , It greatly enhances the real-time and interactivity of the program .

What is long connection 、 Long polling ?

   The simple point is that the client keeps sending requests to the server and then goes to the latest data information . there ' constantly ' In fact, there is a stop . It's just that our eyes can't tell if it stops , It's just a quick stop and start connecting immediately .

Application scenarios

   A long connection 、 Long polling is generally applied to webIM、ChatRoom And some website applications that need timely interaction .web Wechat QR code webQQ HI Web version ,Facebook IM etc.  

Advantages and disadvantages  

   polling : The client sends to the server regularly Ajax request , After receiving the request, the server immediately returns the response information and closes the connection . 
   advantage : Back end programming is easier . 
   shortcoming : Most of the requests are useless , Waste bandwidth and server resources . 
   example : Suitable for small applications .


   Long polling : The client sends to the server Ajax request , The server receives the request hold Live connection , The response information is not returned and the connection is closed until there is a new message , After the client processes the response information, it sends a new request to the server . 
   advantage : Not frequent requests without messages , Low cost of resources . 
   shortcoming : The server hold Connections consume resources , There is no guarantee for the order of data returned , Difficult to manage maintenance . 
   example :WebQQ、Hi Web version 、Facebook IM.

   A long connection : Embed a hidden image in the page iframe, I will hide this iframe Of src Property is set to request a long connection or to adopt xhr request , The server continuously inputs data to the client based on the energy source . 
   advantage : Instant message arrival , No useless requests ; Management is relatively convenient . 
   shortcoming : Server maintenance of a long connection will increase overhead . 
   example :Gmail Chat


  Flash Socket: Embed a usage in the page Socket Class Flash Program JavaScript By calling this Flash Provided by the program Socket Interface and server side Socket Interface to communicate ,JavaScript Control the display of the page after receiving the information sent by the server . 
   advantage : Real instant messaging , Not pseudo instant . 
   shortcoming : Client must be installed Flash plug-in unit ; Not HTTP agreement , Can't automatically cross the firewall . 
   example : Online interactive games .

原网站

版权声明
本文为[Yu Qin]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202200531437014.html

随机推荐