Long Polling与Polling概述 服务端推送技术应用越来越普遍,应用范围也越来越宽广,技术解决方案也越来越成熟且丰富。很多SNS网站的chat功能就有用到了Long Polling技术。比如fackebook, kaixin001。 Long Polling原理其实很简单,也很讨巧。与Polling相比,
服务端推送技术应用越来越普遍,应用范围也越来越宽广,技术解决方案也越来越成熟且丰富。很多SNS网站的chat功能就有用到了Long Polling技术。比如fackebook, kaixin001。
Long Polling原理其实很简单,也很讨巧。与Polling相比,Long Polling客户端也许不会马上收到来自服务端的响应,需要等待一些时间(直到有新消息,或者连接timeout了等等)。同样的,客户端也不再需要定时向服务发送请求了,而是直到收到服务端响应之后,或者连接丢失之后,客户端接着马上请求客户端。这里,我打个比方,传统的Polling一般是由C向S询问:”有我的信件吗?”。S接到询问之后,会立即查询,并且把查询结果告诉C,不管有没有C的信件,要码回复:”嗯,你有X封信。”,要码回复:”没,没有你的信”.而Long Polling更像是这样,C向S发出询问:”有我的信件吗?”,S开始查询,如果有则回复C:”嗯,有你x封信”。如果没有,则不作任何回复,而是让C等着,自己一遍一遍地查询是否有订阅者的信。换句话说:当S收到C的查询请求之后,Polling则只查询一次,并且把查询结果告诉C;而Long Polling收到请求之后,则会一遍一遍地查询,直到有消息才会响应C,不然一直hold Client。
Long Polling相较传统的Polling而言,最大的实惠在于:减少了请求次数。举个例子,假定一个用户每2小时内,有可能收到2条新消息。如果采用传通的Polling方式,每30秒发向服务端发送一次查询请求的话。则在这2小时内,服务器需要处理240(60*60*2/30)次请求,其中至少有238次请求是没有实际意义的。试想,如果是10000的并发量的话,这种浪费是很惊人的。相较而方,Long Polling没有那么浪费服务器资源来处理这些没有实际意义的请求。
传统的Polling实现方式比较单一,由客户端javascript脚本定时发送http请求。服务端脚本如下:
<p> </p><pre class="brush:php;toolbar:false"><span><?php </span> <span>header</span><span>(</span><span>"Expires: Sun, 19 Nov 1978 05:00:00 GMT"</span><span>)</span><span>;</span> <span>header</span><span>(</span><span>"Last-Modified: "</span><span>.</span> <span>gmdate</span><span>(</span><span>"D, d M Y H:i:s"</span><span>)</span> <span>.</span><span>" GMT"</span><span>)</span><span>;</span> <span>header</span><span>(</span><span>"Cache-Control: store, no-cache, must-revalidate"</span><span>)</span><span>;</span> <span>header</span><span>(</span><span>"Cache-Control: post-check=0, pre-check=0"</span><span>,</span> <span>FALSE</span><span>)</span><span>;</span> <span>$msg</span> <span>=</span> get_msg<span>(</span><span>)</span><span>;</span> <span>if</span> <span>(</span><span>$msg</span><span>)</span> <span>{</span> <span>echo</span> <span>$msg</span><span>;</span> <span>}</span> <span>else</span> <span>{</span> <span>echo</span> <span>'0'</span><span>;</span> <span>}</span> <span>?></span></span>
上面是一个传统polling简单的服务端脚本。很简单,收到客户端请求后,服务端马上执行脚本查询,并且立即响应客户端。客户端等待的时间很短,客户端唯一要做的事情就是定时向服务端发出查询请求。下面是请求时,通过tcpdump抓到的包:
1>17:49:03.533760 IP 192.168.0.98.4383 > devhome.http: S 3235664319:3235664319(0) win 65535 <mss> 2>17:49:03.534336 IP devhome.http > 192.168.0.98.4383: S 2018732723:2018732723(0) ack 3235664320 win 5840 <mss> 3>17:49:03.533841 IP 192.168.0.98.4383 > devhome.http: . ack 1 win 65535 4>17:49:03.534404 IP 192.168.0.98.4383 > devhome.http: P 1:781(780) ack 1 win 65535 5><strong>17:49:03.534416 </strong>IP devhome.http > 192.168.0.98.4383: . ack 781 win 7020 6><strong>17:49:03.535033</strong> IP devhome.http > 192.168.0.98.4383: P 1:369(368) ack 781 win 7020 7>17:49:03.535110 IP devhome.http > 192.168.0.98.4383: F 369:369(0) ack 781 win 7020 8>17:49:03.535263 IP 192.168.0.98.4383 > devhome.http: . ack 370 win 65167 9>17:49:03.536105 IP 192.168.0.98.4383 > devhome.http: F 781:781(0) ack 370 win 65167 10>17:49:03.536111 IP devhome.http > 192.168.0.98.4383: . ack 782 win 7020 </mss></mss>
第1、2、3行,TCP三次握手,建立连接。
第4行,由192.168.0.98向服务端devhome发送httpd请求。
第5行,由服务端devhome确认收到了来自客户端192.168.0.98的http请求。
第6行,服务器响devhom响应客户端192.168.0.98刚才发的httpd请求。注意:特别注意一下第一列时间截,http请求与http响应的时间间隔很短,才0.001s
第7、8、9、10共4行,TCP四次挥手,断开连接。由服务端主动断开连接。
Long Polling较之Polling稍微有些不一样,Long Polling持续执行,以此延迟对客户端的响应。请查看代码:
<p> </p><pre class="brush:php;toolbar:false"><span><?php </span> <span>header</span><span>(</span><span>"Expires: Sun, 19 Nov 1978 05:00:00 GMT"</span><span>)</span><span>;</span> <span>header</span><span>(</span><span>"Last-Modified: "</span><span>.</span> <span>gmdate</span><span>(</span><span>"D, d M Y H:i:s"</span><span>)</span> <span>.</span><span>" GMT"</span><span>)</span><span>;</span> <span>header</span><span>(</span><span>"Cache-Control: store, no-cache, must-revalidate"</span><span>)</span><span>;</span> <span>header</span><span>(</span><span>"Cache-Control: post-check=0, pre-check=0"</span><span>,</span> <span>FALSE</span><span>)</span><span>;</span> <span>//在$timeout之后,关闭连接,并且要求客户3秒后重新请求</span> <span>for</span> <span>(</span><span>$i</span> <span>=</span> 0<span>,</span> <span>$timeout</span> <span>=</span> <span>60</span><span>;</span> <span>$i</span> <span> <span>$timeout</span><span>;</span> <span>$i</span><span>++</span> <span>)</span> <span>{</span> <span>$msg</span> <span>=</span> get_msg<span>(</span><span>)</span><span>;</span> <span>if</span> <span>(</span><span>$msg</span><span>)</span> <span>{</span> <span>echo</span> <span>json_encode</span><span>(</span><span>array</span><span>(</span><span>'t'</span> <span>=></span> <span>'info'</span> <span>,</span> <span>'c'</span> <span>=></span> <span>$msg</span><span>)</span><span>)</span><span>;</span> <span>flush</span><span>(</span><span>)</span><span>;</span> <span>exit</span><span>(</span>0<span>)</span><span>;</span> <span>}</span> <span>usleep</span><span>(</span>3000000<span>)</span><span>;</span> <span>}</span> <span>echo</span> <span>json_encode</span><span>(</span><span>array</span><span>(</span><span>'t'</span> <span>=></span> <span>'refresh'</span><span>,</span> <span>'c'</span> <span>=></span> 3000<span>)</span><span>)</span><span>;</span> <span>flush</span><span>(</span><span>)</span><span>;</span> <span>?></span></span></span>
上面是Long Polling服务端代码。语意也很明了,如果有$msg,则会马上响应客户端请求,并且关闭该TCP连接。如果在$timeout之内,没有$msg,则会让客户端一直保持该TCP连接,不中断(关闭)。直到超过了$timeout(具体时间主要取决于$timeout * $usleep_time),服务端会要求客户端重新请求(重新建立TCP连接),同时关闭当前TCP连接。下面是通过 tcpdump抓到的包:
1>18:39:46.449563 IP 192.168.0.98.4407 > devhome.http: S 174149200:174149200(0) win 65535 <mss> 2>18:39:46.449587 IP devhome.http > 192.168.0.98.4407: S 938669730:938669730(0) ack 174149201 win 5840 <mss> 3>18:39:46.449692 IP 192.168.0.98.4407 > devhome.http: . ack 1 win 65535 4>18:39:46.450308 IP 192.168.0.98.4407 > devhome.http: P 1:793(792) ack 1 win 65535 5><strong>18:39:46.450320 </strong>IP devhome.http > 192.168.0.98.4407: . ack 793 win 7128 6><strong>18:42:46.521749</strong> IP devhome.http > 192.168.0.98.4407: P 1:377(376) ack 793 win 7128 7>18:42:46.521825 IP devhome.http > 192.168.0.98.4407: P 377:412(35) ack 793 win 7128 8>18:42:46.521859 IP devhome.http > 192.168.0.98.4407: F 412:412(0) ack 793 win 7128 9>18:42:46.521997 IP 192.168.0.98.4407 > devhome.http: . ack 412 win 65124 10>18:42:46.522021 IP 192.168.0.98.4407 > devhome.http: . ack 413 win 65124 11>18:42:46.522965 IP 192.168.0.98.4407 > devhome.http: F 793:793(0) ack 413 win 65124 12>18:42:46.522970 IP devhome.http > 192.168.0.98.4407: . ack 794 win 7128 </mss></mss>
第1、2、3行,TCP三次握手,建立连接。
第4行,由192.168.0.98向服务端192.168.0.6发送http请求。
第5行,由192.168.0.6确认收到192.168.0.98刚刚发送的请求。
第6、7行,服务器host_6响应3分钟前客户端192.168.0.98发出的http请求。注意,第一列的时间截,第5行与第6行之差为3分钟。这与服务端脚本,客户端监控是相呼应的。
最后4行,TCP四次挥手,断开连接,同样由服务端devhome发起。
由上图可见(httpwatch绘制),正好验证了,响应客户端host_98的http请求,在3分钟之后。这也说明了,Long Polling与Polling的区别在于,客户端有可能需要等待更长时间才能收到服务端的响应。