PHP JavaScript Comet

Source: Internet
Author: User

Tag: Stat One URL does not have Help alert string OSC response

Simple description:

Comet is a server push implemented with Ajax, there are two ways to implement comet, long polling and streaming, where only long polling is implemented.

Long polling process: The page initiates a server request, and the server keeps the connection open until there is data returned. After the data is returned, the browser closes the connection and then initiates another server request. This process is maintained continuously during page opening.

This approach saves bandwidth, and recursive requests (in order) are much better than normal polling disorder.

Testpush.html, the contents are as follows

Simple description:

Comet is a server push implemented with Ajax, there are two ways to implement comet, long polling and streaming, where only long polling is implemented.

Long polling process: The page initiates a server request, and the server keeps the connection open until there is data returned. After the data is returned, the browser closes the connection and then initiates another server request. This process is maintained continuously during page opening.

This approach saves bandwidth, and recursive requests (in order) are much better than normal polling disorder.

Testpush.html, the contents are as follows

?
12345678910111213141516171819202122232425262728293031323334353637383940 <html>    <head>        <metahttp-equiv="pragma"content="no-cache">        <metahttp-equiv="cache-control"content="no-cache">        <metahttp-equiv="Content-Type"content="text/html; charset=utf-8"/>        <scripttype="text/javascript"src="jquery.min.js"></script>               <scripttype="text/javascript">            $(function () {                (function longPolling() {                    alert(Date.parse(new Date())/1000);                    $.ajax({                        url: "testPush1111.php",                        data: {"timed": Date.parse(new Date())/1000},                        dataType: "text",                        timeout: 5000,//5秒超时,可自定义设置                        error: function (XMLHttpRequest, textStatus, errorThrown) {                            $("#state").append("[state: " + textStatus + ", error: " + errorThrown + " ]<br/>");                            if (textStatus == "timeout") { // 请求超时                                longPolling(); // 递归调用                            } else { // 其他错误,如网络错误等                                longPolling();                            }                        },                        success: function (data, textStatus) {                            $("#state").append("[state: " + textStatus + ", data: { " + data + "} ]<br/>");                                                        if (textStatus == "success") { // 请求成功                                longPolling();                            }                        }                    });                })();            });        </script>    </head>    <body>        <divid="state"></div>    </body></html>

testpush.php, the contents are as follows

Test analysis

There are several situations:

1. Successful return, status code 200, and then start the long connection again

2. Timeout, cancel (canceled) This long connection, and then start the long connection again

3. Long connection Waiting (pending), server response

testpush.php, the contents are as follows

?
123456789101112131415161718 <?phpif(!$_GET[‘timed‘]) exit();date_default_timezone_set("PRC");set_time_limit(0);//无限请求超时时间 $timed= $_GET[‘timed‘];while(true) {    sleep(3); // 休眠3秒,模拟处理业务等    $i= rand(0,100); // 产生一个0-100之间的随机数    if($i> 20 && $i< 56) { // 如果随机数在20-56之间就视为有效数据,模拟数据发生变化        $responseTime= time();        // 返回数据信息,请求时间、返回数据时间、耗时        echo("result: ". $i. ", response time: " . $responseTime. ", request time: ". $timed. ", use time: ". ($responseTime- $timed));        exit();    } else{ // 模拟没有数据变化,将休眠 hold住连接        sleep(13);         exit();    }}

Test analysis

There are several situations:

1. Successful return, status code 200, and then start the long connection again

2. Timeout, cancel (canceled) This long connection, and then start the long connection again

3. Long connection Waiting (pending), server response

PHP JavaScript Comet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.