Advertisement
Guest User

Untitled

a guest
Dec 28th, 2016
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.50 KB | None | 0 0
  1.    /**
  2.      * @param Request $request
  3.      * @return \Symfony\Component\HttpFoundation\Response
  4.      * @Route("/checkNotyfication",name="lastNotification")
  5.      */
  6.     public function getLastNotyficationMessageAction(Request $request)
  7.     {
  8.         if ($request->isXmlHttpRequest()) {
  9.  
  10.             $file = $this->get('kernel')->getRootDir() . '/../web/dane.txt';
  11. //
  12.  
  13.             // main loop
  14.             while (true) {
  15.                 // if ajax request has send a timestamp, then $last_ajax_call = timestamp, else $last_ajax_call = null
  16.                 $last_ajax_call = isset($_GET['timestamp']) ? (int)$_GET['timestamp'] : null;
  17.                 // PHP caches file data, like requesting the size of a file, by default. clearstatcache() clears that cache
  18.                 clearstatcache();
  19.                 // get timestamp of when file has been changed the last time
  20.                 $last_change_in_data_file = filemtime($file);
  21.                 // if no timestamp delivered via ajax or data.txt has been changed SINCE last ajax timestamp
  22.                 if ($last_ajax_call == null || $last_change_in_data_file > $last_ajax_call) {
  23.                     // get content of data.txt
  24.                     $data = file_get_contents($file);
  25.                     // put data.txt's content and timestamp of last data.txt change into array
  26.                     $result = array(
  27.                         'data_from_file' => $data,
  28.                         'timestamp' => $last_change_in_data_file
  29.                     );
  30.                     // encode to JSON, render the result (for AJAX)
  31.                     $json = json_encode($result);
  32.                     echo $json;
  33.                     // leave this loop step
  34.                     break;
  35.                 } else {
  36.                     // wait for 1 sec (not very sexy as this blocks the PHP/Apache process, but that's how it goes)
  37.                     sleep(1);
  38.                     continue;
  39.                 }
  40.  
  41.             }
  42.  
  43.  
  44. //            $currentTimeStmp = filemtime($file);
  45. //
  46. //            $lastDate = isset($_POST['timestamp']) ? $_POST['timestamp'] : 0;
  47. //
  48. //            while ($currentTimeStmp <= $lastDate) {
  49. //                usleep(1e6); // uspanie na sekundÄ™
  50. //                clearstatcache();
  51. //                $currentTimeStmp = filemtime($file);
  52. //            }
  53. //
  54. //            $array = [];
  55. //
  56. //            $array['timestamp'] = $currentTimeStmp;
  57. //            return new JsonResponse($array);
  58.  
  59.  
  60.         }
  61.  
  62.  
  63.         return new Response();
  64.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement