PHP后台通知

Notification on background PHP

本文关键字:通知 后台 PHP      更新时间:2023-09-26

我有一个web应用程序,它使用通知来通知用户任何新信息(就像Facebook一样)。

我的解决方案是,我每三秒发送一个请求,检查数据库是否有新的显示内容(jQuery和AJAX)。但是,这会使应用程序速度变慢,因为每隔三秒就会向检查表发送一个请求。

我想知道如何在不中断应用程序的情况下使这些通知正常工作。
这是我的JS代码:

jQuery(document).ready(function(){    
    LoopNotificationCRM();
  });
    function LoopNotificationCRM(){
  setTimeout('LoopNotificationCRM();',3000);
  $.ajax({
            async: false,
            type: "POST",
            url: "controllers/c_ajax_notification.php",
            data: "ordre=check_new_notification",
            success: function(msg){
              if(msg != 'NAN'){
               var t = msg.split('***');
               $('.sNotification').html(t[0]);
               $('.ul-notification').html(t[1]);
               $('.alert-notification').css('display','block');
              }else{
               $('.sNotification').html(0);
               $('.alert-notification').css('display','none');
               $('.ul-notification').html('');
              }
            }, 
          error: function (xhr, status) {  
            alert('Erreur: ' + status); 
            } 
         });
}

这是我的PHP代码:

$notification->getNewNotification("*");
         if($notification->db_num_row != 0){
                      $listNoti = '';
                      while($resN = $notification->fetch_array()){
                       $today = new DateTime(date('Y-m-d H:i:s'));
                       $datNoti = new DateTime($resN['date_not_crm']);
                       $diff = $datNoti->diff($today);
                       if($diff->d == 0){
                         if($diff->h == 0){
                           if($diff->i == 0){
                              $intervale = 'il y a '.$diff->s.' sec';
                           }else{
                              $intervale = 'il y a '.$diff->i.' min';
                           }                           
                         }else{
                           $intervale = 'il y a '.$diff->h.' heure(s)';
                         }
                       }else{
                           $intervale = 'il y a '.$diff->d.' jour(s)';
                       }
       $listNoti .= '<li>  
                                 <a onclick="link(event,'''.$resN['url_not_crm'].''');updateEtatNoti(this,'.$resN['id_not_crm'].');" style="cursor:pointer;">
                                 <span class="label label-icon label-success"><i class="'.$resN['icon_not_crm'].'"></i></span>
                                 '.$notification->csNotification($resN['description_not_crm']).' 
                                 <span class="time">'.$intervale.'</span>
                                 </a>
                              </li>';
                     }
            echo $notification->getCountNewNotification().'***'.$listNoti;
        }else{
         echo 'NAN';
        }

当我删除通知代码时,我的应用程序会变得更快!

如果您的应用程序在每3秒运行一次ajax时速度减慢,请尝试简化/缩小ajax加载页面所需的工作量。例如,不要读取整个表,而是尝试只选择"已读"状态为0的通知。这样一来,php文件的执行速度更快,这意味着ajax不会降低页面的速度。