Php+jquery implementation Information push

Source: Internet
Author: User

Background: Always feel that information push is a very useful function. Previously thought of the original implementation of the scheme is: using AJAX interval time to request a background, from the background to remove the most recent messages as a return value to the AJAX callback display. Today I saw a different solution: in the PHP side through the while to determine whether the information has changed, there are returned to the foreground display, after the display to continue to send AJAX requests to the PHP background monitoring information; When not, PHP side while to keep the loop monitoring information State, This is not the end of the request, the Ajax state will not be completed, there will be no need to resend the request.

The two schemes were later analyzed, with the following general results

The first interval is sent once, because the request frequency of the problem, will inevitably lead to more requests. But this kind of request will quickly open up, do not need to monitor information changes, only to focus on the request in the information.

The second is that the PHP side polls the monitoring information through a while polling, the end of the request, return to the Ajax display, and then send the next monitoring request; The while loop is always monitored. This scenario will greatly reduce the number of requests, but will result in each user corresponding to a server-side PHP monitoring business. In addition, PHP is not a multithreaded concept, with Apache there is a limit to the maximum number of access to the set item Maxranges. These problems can cause some server-side access to the problem of excessive traffic.

The third way to listen to colleagues is that Strophe.js is used to solve web IM's encapsulated plug-ins. (After the application is written)

Remark: The above two kinds of scheme, the concrete application, may still need to see the scene choice

The code for the first scenario is abbreviated (mostly While,settimeout,ajax, recursive calls, and so on). The code for the second application is much the same as the following.

The second type of code:

<?php//Set request running time is not limited to resolve the request Ini_set ("Max_execution_time", "0") because it exceeds the server running time; $filename = DirName (__file__). '
/data.txt '; $msg = isset ($_get[' msg '])?

$_get[' msg ']: ';
    To determine whether the modification of the page submission is null, is not empty, writes the content to the file, and interrupts the process if ($msg!= ') {file_put_contents ($filename, $msg);
Exit /* Get file Last modified timestamp and the most recent file modification timestamp * File Last modified timestamp initial default value is 0 * Last file modification timestamp via function filemtime () get * * $lastmodif = isset ($ _get[' timestamp '])?
$_get[' timestamp ': 0;  Clearstatcache ();

Clears the file state cache $currentmodif = Filemtime ($filename); /* If the currently returned file modifies the Unix timestamp less than or equal to the last modified time, * indicates that the file is not updated and does not need to push the message * If the currently returned file modifies the Unix timestamp greater than the last modification time * indicates that the file has an update and needs to output the modified content as a     $currentmodif <= $lastmodif) {usleep (10000);  Hibernate 10ms frees up the CPU's occupancy clearstatcache ();
Clears the file state cache $currentmodif = Filemtime ($filename);
//Push information processing (need to push the description file has changes, push the message contains this time of modification, content) $response = Array ();
$response [' msg '] = file_get_contents ($filename);
$response [' timestamp '] = $currentmodif;
echo Json_encode ($response);
 Flush ();?>
HTML page code
<! DOCTYPE HTML PUBLIC "-//w3c//dtd XHTML 1.1//en" "Http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd" > Need to import JS resource: Jquery,json2.js


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.