Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * Server-side file.
- * This file is an infinitive loop. Seriously.
- * It gets the file data.txt's last-changed timestamp, checks if this is larger than the timestamp of the
- * AJAX-submitted timestamp (time of last ajax request), and if so, it sends back a JSON with the data from
- * data.txt (and a timestamp). If not, it waits for one seconds and then start the next while step.
- *
- * Note: This returns a JSON, containing the content of data.txt and the timestamp of the last data.txt change.
- * This timestamp is used by the client's JavaScript for the next request, so THIS server-side script here only
- * serves new content after the last file change. Sounds weird, but try it out, you'll get into it really fast!
- */
- // set php runtime to unlimited
- set_time_limit(0);
- // where does the data come from ? In real world this would be a SQL query or something
- $data_source_file = 'data.txt';
- // main loop
- while (true) {
- // if ajax request has send a timestamp, then $last_ajax_call = timestamp, else $last_ajax_call = null
- $last_ajax_call = isset($_GET['timestamp']) ? (int)$_GET['timestamp'] : null;
- // PHP caches file data, like requesting the size of a file, by default. clearstatcache() clears that cache
- clearstatcache();
- // get timestamp of when file has been changed the last time
- $last_change_in_data_file = filemtime($data_source_file);
- // if no timestamp delivered via ajax or data.txt has been changed SINCE last ajax timestamp
- if ($last_ajax_call == null || $last_change_in_data_file > $last_ajax_call) {
- // get content of data.txt
- $data = file_get_contents($data_source_file);
- // put data.txt's content and timestamp of last data.txt change into array
- $result = array(
- 'data_from_file' => $data,
- 'timestamp' => $last_change_in_data_file
- );
- // encode to JSON, render the result (for AJAX)
- $json = json_encode($result);
- echo $json;
- // leave this loop step
- break;
- } else {
- // wait for 1 sec (not very sexy as this blocks the PHP/Apache process, but that's how it goes)
- sleep( 1 );
- continue;
- }
- }
- /**
- * AJAX long-polling
- *
- * 1. sends a request to the server (without a timestamp parameter)
- * 2. waits for an answer from server.php (which can take forever)
- * 3. if server.php responds (whenever), put data_from_file into #response
- * 4. and call the function again
- *
- * @param timestamp
- */
- function getContent(timestamp)
- {
- var queryString = {'timestamp' : timestamp};
- $.ajax(
- {
- type: 'GET',
- url: '../server/server.php',
- data: queryString,
- success: function(data){
- // put result data into "obj"
- var obj = jQuery.parseJSON(data);
- // put the data_from_file into #response
- $('#response').html(obj.data_from_file);
- // call the function again, this time with the timestamp we just got from server.php
- getContent(obj.timestamp);
- }
- }
- );
- }
- // initialize jQuery
- $(function() {
- getContent();
- });
- <html>
- <head>
- <script type="text/javascript" src="../../../../JS/jquery.min.js"></script>
- <script type="text/javascript" src="client.js"></script>
- </head>
- <body>
- <h1>Response from server:</h1>
- <div id="response" >
- </div>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement