我在项目中使用服务器推送事件技术,在JS调用PHP页面时,比如说 eventserver.php ,基本上是一个无限循环,它会检查
这实际上会引起很多PHP警告(在Apache error.log中),显示“无法发送会话缓存限制器——头已经被发送”,“无法发送会话cookie”等等。
这里发布一些代码。
为什么会发生这种情况?
$_SESSION
变量中是否存在事件。
在我的第一次实现中,这导致我的网站挂起,因为事件服务器锁定了会话,并且直到超时才释放。然而,我通过在循环中连续使用 session_write_lock()
和 session_start()
来解决了这个问题。这实际上会引起很多PHP警告(在Apache error.log中),显示“无法发送会话缓存限制器——头已经被发送”,“无法发送会话cookie”等等。
这里发布一些代码。
session_start();
header('Cache-Control: no-cache');
header('Content-Type: text/event-stream');
class EventServer
{
public function WaitForEvents( $eventType )
{
// ... do stuff
while( true )
{
// lock the session to this instance
session_start();
// ...check/output the event
ob_flush();
flush();
// unlock the session
session_write_close();
sleep( 1 );
}
}
}
为什么会发生这种情况?