Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * @param Request $request
- * @return \Symfony\Component\HttpFoundation\Response
- * @Route("/checkNotyfication",name="lastNotification")
- */
- public function getLastNotyficationMessageAction(Request $request)
- {
- if ($request->isXmlHttpRequest()) {
- $file = $this->get('kernel')->getRootDir() . '/../web/dane.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($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($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;
- }
- }
- // $currentTimeStmp = filemtime($file);
- //
- // $lastDate = isset($_POST['timestamp']) ? $_POST['timestamp'] : 0;
- //
- // while ($currentTimeStmp <= $lastDate) {
- // usleep(1e6); // uspanie na sekundÄ™
- // clearstatcache();
- // $currentTimeStmp = filemtime($file);
- // }
- //
- // $array = [];
- //
- // $array['timestamp'] = $currentTimeStmp;
- // return new JsonResponse($array);
- }
- return new Response();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement