Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * Server-side file.
  5. * This file is an infinitive loop. Seriously.
  6. * It gets the file data.txt's last-changed timestamp, checks if this is larger than the timestamp of the
  7. * AJAX-submitted timestamp (time of last ajax request), and if so, it sends back a JSON with the data from
  8. * data.txt (and a timestamp). If not, it waits for one seconds and then start the next while step.
  9. *
  10. * Note: This returns a JSON, containing the content of data.txt and the timestamp of the last data.txt change.
  11. * This timestamp is used by the client's JavaScript for the next request, so THIS server-side script here only
  12. * serves new content after the last file change. Sounds weird, but try it out, you'll get into it really fast!
  13. */
  14.  
  15. // set php runtime to unlimited
  16. set_time_limit(0);
  17.  
  18. // where does the data come from ? In real world this would be a SQL query or something
  19. $data_source_file = 'data.txt';
  20.  
  21. // main loop
  22. while (true) {
  23.  
  24. // if ajax request has send a timestamp, then $last_ajax_call = timestamp, else $last_ajax_call = null
  25. $last_ajax_call = isset($_GET['timestamp']) ? (int)$_GET['timestamp'] : null;
  26.  
  27. // PHP caches file data, like requesting the size of a file, by default. clearstatcache() clears that cache
  28. clearstatcache();
  29. // get timestamp of when file has been changed the last time
  30. $last_change_in_data_file = filemtime($data_source_file);
  31.  
  32. // if no timestamp delivered via ajax or data.txt has been changed SINCE last ajax timestamp
  33. if ($last_ajax_call == null || $last_change_in_data_file > $last_ajax_call) {
  34.  
  35. // get content of data.txt
  36. $data = file_get_contents($data_source_file);
  37.  
  38. // put data.txt's content and timestamp of last data.txt change into array
  39. $result = array(
  40. 'data_from_file' => $data,
  41. 'timestamp' => $last_change_in_data_file
  42. );
  43.  
  44. // encode to JSON, render the result (for AJAX)
  45. $json = json_encode($result);
  46. echo $json;
  47.  
  48. // leave this loop step
  49. break;
  50.  
  51. } else {
  52. // wait for 1 sec (not very sexy as this blocks the PHP/Apache process, but that's how it goes)
  53. sleep( 1 );
  54. continue;
  55. }
  56. }
  57.  
  58. /**
  59. * AJAX long-polling
  60. *
  61. * 1. sends a request to the server (without a timestamp parameter)
  62. * 2. waits for an answer from server.php (which can take forever)
  63. * 3. if server.php responds (whenever), put data_from_file into #response
  64. * 4. and call the function again
  65. *
  66. * @param timestamp
  67. */
  68. function getContent(timestamp)
  69. {
  70. var queryString = {'timestamp' : timestamp};
  71.  
  72. $.ajax(
  73. {
  74. type: 'GET',
  75. url: '../server/server.php',
  76. data: queryString,
  77. success: function(data){
  78. // put result data into "obj"
  79. var obj = jQuery.parseJSON(data);
  80. // put the data_from_file into #response
  81. $('#response').html(obj.data_from_file);
  82. // call the function again, this time with the timestamp we just got from server.php
  83. getContent(obj.timestamp);
  84. }
  85. }
  86. );
  87. }
  88.  
  89. // initialize jQuery
  90. $(function() {
  91. getContent();
  92. });
  93.  
  94. <html>
  95. <head>
  96. <script type="text/javascript" src="../../../../JS/jquery.min.js"></script>
  97. <script type="text/javascript" src="client.js"></script>
  98. </head>
  99. <body>
  100. <h1>Response from server:</h1>
  101. <div id="response" >
  102.  
  103.  
  104.  
  105. </div>
  106. </body>
  107. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement