javascript - 带有事件源的 Xdebug

标签 javascript php xdebug eventsource

我在启动 PhpStorm 调试应用程序时遇到了一些问题。

当我访问调试我的应用程序的 URL 时,会为通知系统发出额外的事件源请求 (SSE)。但是当调试工作时它会导致我无法刷新页面的问题,它只是无限期地加载 - 我没有任何断点可以确定。在我停止调试 session 后,它会立即加载。

这是怎么回事?我可以以某种方式禁用这个特定 URL 的 xdebug 吗?

我的这些通知代码如下所示:

public function moreAction()
{
    ob_start();
    $maxId = $this->request->getQuery('maxId', 'int');
    header('Cache-Control: no-cache');
    header("Content-Type: text/event-stream\n\n");
    while (1) {
        $result = $this->notificationService->more($maxId);
        if (count($result) > 0) {
            foreach ($result as &$row) {
                if (!empty($row['data'])) {
                    $row['data'] = json_decode($row['data'], true);
                }
            }
            $maxId = $result[0]['id'];
            echo "event: message\n";
            echo "data:".json_encode($result);
            echo "\n\n";
            $this->doFlush();
        }
        sleep(5);
    }
}

protected function doFlush()
{
    if (!headers_sent()) {
        // Disable gzip in PHP.
        ini_set('zlib.output_compression', 0);
        // Force disable compression in a header.
        // Required for flush in some cases (Apache + mod_proxy, nginx, php-fpm).
        header('Content-Encoding: none');
    }
    // Fill-up 4 kB buffer (should be enough in most cases).
    echo str_pad('', 4 * 1024);
    // Flush all buffers.
    do {
        $flushed = @ob_end_flush();
    } while ($flushed);
    @ob_flush();
    flush();
}

最佳答案

我在使用 xdebug 调试 SSE 时遇到了同样的问题。解决方法是在离开页面时关闭事件源。

假设您在客户端使用 javascript 来请求事件源,例如

let eventSource = new EventSource("moreAction.php")

放点像

$(window).on('beforeunload', function(){
    eventSource.close();
});

在您的页面中。

顺便说一句,在您的事件源循环中使用 while(1) 不是一个好主意,因为它会无限循环,完全符合您正在经历的行为。 30-120 秒范围内的超时是合理的,即使事件源未关闭,您是否允许继续调试。毕竟,它正在自动重新连接。

关于javascript - 带有事件源的 Xdebug,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47099931/

相关文章:

javascript - 使用 Google Apps 脚本的确认消息框

php - 谷歌地图保存优化方向

按钮上的php条件

php - PhpStorm 中带有 Vagrant 和 Xdebug 的恼人警告 "Debug session was finished without being paused"

php - 加载 php_xdebug 消息失败

javascript - XmlHttp Response.responseText 什么时候会填充来自服务器的响应?

javascript - 在 php 和 html5 中使用 javascript 添加行

curl 调用后未保存 PHP session

php - Docker 工具箱 Xdebug 无法与 PhpStorm 一起使用

javascript - Flot JS 将目标变成菱形