Cześć,

testuje sobie SSE do push pingu. Ogólnie działa dobrze, tylko w jaki sposób wykryć ostatnio dodany post? a jezeli nic nie ma to po prostu nie robić nic? kompletnie nie mam pomysłu na jakiej podstawie taki warunek skonstruować?

server

  1. header('Access-Control-Allow-Origin: *');
  2. header('Access-Control-Allow-Methods: GET, POST');
  3. header('Access-Control-Allow-Headers: X-Requested-With');
  4. header('Content-Type: text/event-stream');
  5. header('Cache-Control: no-cache');
  6.  
  7.  
  8.  
  9. while (true) {
  10.  
  11. $stmt = $dbh->query('select * from product ORDER BY id DESC limit 1');
  12. $row = $stmt->fetch();
  13. echo 'data: "' . $row['name'] . '"' . "\n\n";
  14.  
  15. flush();
  16.  
  17. sleep(1);
  18. }


klient
  1.  
  2. if (!!window.EventSource) {
  3. var source = new EventSource('server.php');
  4.  
  5. source.addEventListener('message', function (e) {
  6. $.notify({
  7.  
  8. message: e.data,
  9. }, {
  10. // settings
  11. type: 'info'
  12.  
  13. });
  14. }, false);
  15. }
  16.  



Hej, hej, nikt nie ma pomysłu smile.gif?