Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 24th, 2012  |  syntax: None  |  size: 1.76 KB  |  hits: 41  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Ajax comet - slow open next link
  2. <?php
  3. function my_abort_handler(  ) {
  4.     write2file(connection_status() . ' SD Aborted!!!');
  5. }
  6. register_shutdown_function('my_abort_handler');
  7.  
  8. try {
  9.     error_reporting(0);
  10.     session_start();
  11.  
  12.     if (!isUserLogedIn()) {
  13.         $return['message'] = "Login required";
  14.         $return['error'] = true;
  15.  
  16.         echo json_encode($return);
  17.         exit();
  18.     }
  19.  
  20.     if (empty($_SESSION['return'])) {
  21.         $_SESSION['return'] = false;
  22.     }
  23.  
  24.     connectToDB();
  25.     while (!connection_aborted()) {
  26.         write2file(connection_status() . ' SD Aborted!!!');
  27.         $return = collectDataFromDB();
  28.  
  29.         $d1 = array_diff_assoc($return, $_SESSION['return']);
  30.         $d2 = array_diff_assoc($return, $_SESSION['return']);
  31.         if ((!empty($d1)) || (!empty($d2))) {
  32.             $_SESSION['vote_return'] = $return;
  33.             echo json_encode($return);
  34.             flush();
  35.             ob_flush();
  36.             exit();
  37.         }
  38.  
  39.         sleep(1);
  40.     }
  41. } catch (Exception $e) {
  42.     $return['message'] = $e->getMessage();
  43.     $return['error'] = true;
  44.  
  45.     echo json_encode($return);
  46. }
  47.        
  48. var xhReq = false;
  49. function runComet() {
  50.     xhReq = GetXmlHttpObject();
  51.     xhReq.open("GET",'backend.php',true);
  52.     xhReq.onreadystatechange=consoleinfo;
  53.     xhReq.send(null);
  54. }
  55.  
  56. function consoleinfo() {
  57.     if (xhReq.readyState == 4) {
  58.       if (xhReq.status == 200) {
  59.           console.info(xhReq.responseText);
  60.       } else {
  61.           console.info("Error Status:" + xhReq.status);
  62.       }
  63.       runComet();
  64. }
  65.  
  66. runComet();
  67.        
  68. tcp        0      0 127.0.0.1:35518         127.0.0.1:80            ESTABLISHED 2660/firefox     off (0.00/0/0)
  69.        
  70. tcp        1      0 127.0.0.1:80            127.0.0.1:35518         CLOSE_WAIT  3174/httpd       keepalive (7167.02/0/0)