当服务器发送的事件正在运行时,带有 session_start() 的页面不会加载 [英] Pages with session_start() don't load when server-sent event is running

查看:48
本文介绍了当服务器发送的事件正在运行时,带有 session_start() 的页面不会加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个小项目来制作 1v1 聊天系统.我想从事一个可以充分利用 Server-Sent Events 的项目.它运行良好,但最近我对代码进行了一些更改,以便总体上提高聊天效率.

I'm working on a little project to make a 1v1 chat system. I wanted to work on a project where I could put Server-Sent Events to good use. It's been working pretty well, but recently I have been making some changes to the code so that in general the chat would be more efficient.

当我运行事件源时,我遇到了一个奇怪的问题.当我尝试转到具有 session_start() 的任何其他 PHP 页面时,它不会加载.

When I have the event source running I run into a weird issue. When I try to go to any other PHP page that has session_start() it doesn't load.

我的事件源脚本如下所示:

My event source script looks something like this:

<?php
    session_start();

    require "connect.php";
    require "user.php";

    header("Content-Type: text/event-stream\n\n");
    header('Cache-Control: no-cache');

    set_time_limit(1200);

    $ms = 5000;
    //SOME MORE VARIABLES HERE

    while (1) {
        echo "data: SSESSION " . $_SESSION["conversation" . $_GET["id"]];
        echo "\n\n";

            /*CHAT SCRIPT HERE*/

        ob_flush();
        flush();
        usleep($ms * 1000);
    }
?>

会话应该返回的是会话的行数.确实如此.

What the session should return is the number of rows for the conversation. It does indeed.

如果我去test.php:

<?php
    session_start();
    echo $_SESSION["conversation121643"];
?>

Server-Sent Event 正在运行时,此页面将继续加载,直到我取消该事件.

While the Server-Sent Event is running, this page just continues loading until I cancel the event.

我尝试使用 ob_flush 函数,但这并不能解决任何问题.

I tried using the ob_flush function, but that doesn't fix anything.

我不知道为什么我会遇到这个问题.我希望有一个修复它.

I have no clue why i'm having this issue. And I hope there is a fix for it.

推荐答案

PHP 在 session 周围有独占锁.一次只有一个进程可以使用它.这通常不会引起注意,因为 PHP 进程快速启动和结束,但使用 SSE,您将永远保持会话打开,所有其他 PHP 进程将等到 SSE 进程结束.

PHP has exclusive lock around session. Only one process at a time can use it. This is usually unnoticeable because PHP processes start and end quickly, but with SSE you're keeping the session open forever and all other PHP processes will wait until SSE process ends.

您可以执行 session_write_close() 将会话释放给其他进程.

You can execute session_write_close() to release the session to other processes.

这篇关于当服务器发送的事件正在运行时,带有 session_start() 的页面不会加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆