Blogger Information
Blog 14
fans 0
comment 0
visits 15033
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
php长连接
krasenChen的博客
Original
1655 people have browsed it

//在服务器端hold住一个连接,不立即返回,直到有数据才返回。

//key_point :hold住一个HTTP请求,直到有新数据时才响应请求,然后C端再次自动发动长连接请求

//how to do  service  服务器端看起来像这样子

set_time_limit(0);         //这句很重要,不至于运行超时

while(true) {

    if (hasNewMessage()) {

    echo json_encode(getNewMessage()) ;

    break;

}

    usleep(100000);          //避免太过于频繁的查询

}

//从上面就看到是通过循环来实现hold住一个请求,不至于立即返回。查询到有新数据后才响应请求,然后C端处理数据后

//再次发起长连接请求

//how to do client C端

<script type="text/javascript">

     (function longPolling() {

    $.ajax ({

    'url':   'server.php',

    'data':  data,

    'dataType': 'json',

     'success': function(data) {

     processData(data);

    longPolling();

},

    'error': function(data) {

      longPolling();  

},

});

}) ();


</script>

Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post