dtxa49711 2014-04-14 15:34
浏览 37
已采纳

新消息的通知。 长期民意调查

Help me please to realise notifications of new messages for users.

Now i have this client code:

function getmess(){
$.ajax({
    url:"notif.php",
    data:{"id":id},
    type:"GET",
    success:function(result){
        $("#count").html(result);
        setTimeout('getmess',10000);
    }
  });
}

and this server code:

    $mysqli = new mysqli('localhost', 'root', '', 'test');
if (mysqli_connect_errno()) { 
    printf("error: %s
", mysqli_connect_error()); 
    exit; 
}  

session_start();

$MY_ID = $_SESSION['id'];

while (true) {
$result = $mysqli->query("SELECT COUNT(*) FROM messages WHERE user_get='$MY_ID'");
if (mysqli_num_rows($result)) {
while ($row = mysqli_fetch_array($result)) {
echo $row[0]."";
 }
flush();
exit;
}
sleep(5);
}

I have the problem that this script is not updating in real time when new message was added to database. But if I press button with onclick="getmess();" it works.

  • 写回答

1条回答 默认 最新

  • douduan9129 2014-04-14 16:34
    关注

    First, you check your database every 5 seconds, so you can't achieve real time - you have at least 5 seconds delay.

    And second, there is no way you can achieve real-time by polling.

    The way to deliver notifications nearly real time is to send the message by the same code that inserts into the database, e.g. you should not query the database for new records, but when there is a new record to send the data to the client. Even with a long-polling as a transport protocol.

    How to achieve this? Unfortunately PHP is not a good choice. You need a non-blocking server to hold the connection, you need to know which connection waits for what data and you need a way from PHP (your backend) to notify this connection.

    You can use the tornado-web server, node.js or nginx to handle the connections. You assign an identifier to each connection (probably you already have one - the userid), and when there is a new record added - the PHP script performs HTTP request to the notification server (tornado, node.js, nginx) saying what data to which user does this.

    For nginx, take a look at nginx push stream

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi