First, I would like to appreciate all of you, great people, for being so useful to new programmers.
I have a question about a lengthy survey. I studied some articles about the long comet polling method . The method seems very difficult to me because sometimes it is necessary to install some scripts on the server side.
Now I found an example with a lengthy survey. It works great, but I'm not sure if this is the correct method. The sample script refers to a chat-like application. This PHP script works as follows:
- The php script constantly checks the data.txt file until it is modified.
- As soon as the data.txt file changes, new text is displayed on the web page.
Here is the php script:
<?php
$filename = dirname(__FILE__).'/data.txt';
$msg = isset($_GET['msg']) ? $_GET['msg'] : '';
if ($msg != '')
{
file_put_contents($filename,$msg);
die();
}
$lastmodif = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
$currentmodif = filemtime($filename);
while ($currentmodif <= $lastmodif)
{
usleep(500000);
clearstatcache();
$currentmodif = filemtime($filename);
}
$response = array();
$response['msg'] = file_get_contents($filename);
$response['timestamp'] = $currentmodif;
echo json_encode($response);
flush();
?>
-, . - div, data.txt , .
:
- looping ?
- ,
sleep();, ? - - script?
- , , , script ?
...