How to Use Long Polling or Ajax Push in your Websi

2020-03-26 04:52发布

I want to know how to use ajax push . i have learnt from various Web articles that Ajax push can be obtained by using few programmes like COMET, APE (AJAX PUSH ENGINE) etc.... But i want to know whether there is a simpler way of using it and what language is used to implement ajax push. because in the articles which i have seen. they are using java. which i did not learn :( so i would like to know whether there is something like : a javascript in your server which sets an interval to a particular item and then if any changes found then echo it out using php. ? please help me out for this . its been a week now i tried to achieve this. i tried to use normal ajax and php by using intervals but not able to get the result. Thank you. P.S : Please show me an easy way of using it with an example or something.

2条回答
贼婆χ
2楼-- · 2020-03-26 05:38

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

查看更多
3楼-- · 2020-03-26 05:38

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).

查看更多
登录 后发表回答