How to Use Long Polling or Ajax Push in your Website

可紊 提交于 2019-12-01 08:37:21

I used a very simple approach based on flash some time ago

I included a little 1px*1px transparent flash on my page that opened a socket to the server my AJAX sends requests to. The server receives the AJAX request and responds on the flash socket

The flash just opens a javascript: url that calls an onreceive event handler, so you won't open a new page but run the javascript on your current page

leggetter

If you want to use PHP as your backend technology then it's going to be an uphill struggle. Have a read through this question on concurrency - How to implement event listening in PHP for more information.

The simplest solution for PHP developers in my opinion is to use a hosted realtime service like Pusher - who I work for. This means you don't need to worry about the installation or maintenance of your realtime web infrastructure and most importantly you don't need to worry about your server handling persistent concurrent connections. You use the Pusher JavaScript library connects to Pusher from the web browser, maintains a persistent connection and receives any updates pushed to it and the Pusher REST API to publish data from your PHP app, through Pusher, to the connected clients.

There's a getting started with Pusher guide on Nettuts+ which has been very popular and is a good starting point for anybody using PHP.

If you really want to host your own realtime infrastructure on PHP (don't say I haven't warned you) then you can look at How to implement PHP with Comet and PHP WebSockets (there's also a project on github with recent activity called php-websocket-server).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!