Php 使用SSE检查数据库更改

Php 使用SSE检查数据库更改,php,server-sent-events,Php,Server Sent Events,正在尝试创建一个小型通知系统。当用户填写配置文件时,他的验证状态在数据库中设置为1,然后我想显示一个通知“嘿,您现在已验证”。我在网上搜索了很多东西,但是没有任何东西能帮助我达到目标。如果数据库中的状态为1,我在test.php中得到事件:verification\u ok,但如果为0,我得到超过120秒的最大执行时间。此外,我在客户端代码中没有看到任何响应 这是服务器端代码(test.php) 这是一次性通知吗?您将发送一次验证成功/失败消息,然后再也不需要通过SSE连接发送任何东西了?(如果

正在尝试创建一个小型通知系统。当用户填写配置文件时,他的验证状态在数据库中设置为1,然后我想显示一个通知“嘿,您现在已验证”。我在网上搜索了很多东西,但是没有任何东西能帮助我达到目标。如果数据库中的状态为1,我在
test.php
中得到事件:verification\u ok,但如果为0,我得到
超过120秒的最大执行时间。此外,我在客户端代码中没有看到任何响应

这是服务器端代码(test.php)


这是一次性通知吗?您将发送一次验证成功/失败消息,然后再也不需要通过SSE连接发送任何东西了?(如果要保持开放,因为会发送其他消息,可能需要给出一些示例,这样人们给出的答案会更有用。)
<?php
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
header("Connection: Keep-alive");
require_once $_SERVER['DOCUMENT_ROOT'].'/PHP/scripts/no_session_redirect.php';
$key = true;

$ver = $user_home->runQuery("SELECT verification_status FROM verification WHERE user_id=:user_id");
$ver->execute(array(":user_id"=>$user_id));
$status = $ver->fetch(PDO::FETCH_ASSOC);

    while($key){
        if($status["verification_status"] == 1){
          pushNotification($status["verification_status"]);
           $key = false;
         }else{
           $status["verification_status"];

        sleep(10);

      }
    }


function pushNotification() {
  echo "Event: verification_ok\n";
}
        $(document).ready(function() {

        if (typeof(EventSource) !== "undefined") {
            // Yes! Server-sent events support!
            var source = new EventSource("test.php");
            source.addEventListener("verification_ok", function(e) {
                console.log(e.data);
            }, false);

            source.addEventListener("open", function(e) {

            }, false);

            source.addEventListener("error", function(e) {
                if (e.readyState == EventSource.CLOSED) {
                    console.log("Error - connection was lost.");
                }
            }, false);
        } else {
            // Sorry! No server-sent events support..
        }
    });