search
Homephp教程php手册PHP服务端推送技术Long Polling

Long Polling与Polling概述 服务端推送技术应用越来越普遍,应用范围也越来越宽广,技术解决方案也越来越成熟且丰富。很多SNS网站的chat功能就有用到了Long Polling技术。比如fackebook, kaixin001。 Long Polling原理其实很简单,也很讨巧。与Polling相比,

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

传统的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

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发起。

PHP服务端推送技术Long Polling

由上图可见(httpwatch绘制),正好验证了,响应客户端host_98的http请求,在3分钟之后。这也说明了,Long Polling与Polling的区别在于,客户端有可能需要等待更长时间才能收到服务端的响应。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.