阿帕奇 + SSE + PHP
Apache + SSE + PHP
我正在使用 SSE 将一些数据发送到浏览器(网络聊天)。我遇到了一些问题。 SSE 工作正常,但其他请求没有。所有请求都停留在等待状态很长时间,甚至点击 link 到另一个页面也不起作用,直到按下浏览器停止按钮。
我在 PHP 5.4.4、5.4.45 和 Apache 2.4 和 2.2 上对其进行了测试...结果完全相同。我试图在 apache.conf 中更改 mpm 设置,但没有任何改变。任何人有什么想法可以帮助我吗?
这是来自控制器的操作:
/**
* @return string
*/
public function actionIndex()
{
/** @var SSE $sse */
$sse = \Yii::$app->sse;
$sse->addEventListener('message', new MessageEventHandler());
$sse->start();
}
这是消息处理程序:
class MessageEventHandler extends SSEBase
{
public function check()
{
return true;
}
public function update()
{
return 'New message!';
}
}
浏览器端:
var sseObject = $.SSE('/notifier', {
events: {
chat_message: function (e) {
console.log(e.data);
}
}
});
sseObject.start();
问题可能与会话锁定有关。
PHP writes its session data to a file by default. When a request is made to a PHP script that starts the session (session_start()), this session file is locked. What this means is that if your web page makes numerous requests to PHP scripts, for instance, for loading content via Ajax, each request could be locking the session and preventing the other requests from completing.
结合 SSE 连接是持久的,因此会话将保持锁定状态。
http://konrness.com/php5/how-to-prevent-blocking-php-requests/
可能的解决方案是在 actionIndex()
中调用 session_write_close()
。
我正在使用 SSE 将一些数据发送到浏览器(网络聊天)。我遇到了一些问题。 SSE 工作正常,但其他请求没有。所有请求都停留在等待状态很长时间,甚至点击 link 到另一个页面也不起作用,直到按下浏览器停止按钮。 我在 PHP 5.4.4、5.4.45 和 Apache 2.4 和 2.2 上对其进行了测试...结果完全相同。我试图在 apache.conf 中更改 mpm 设置,但没有任何改变。任何人有什么想法可以帮助我吗?
这是来自控制器的操作:
/**
* @return string
*/
public function actionIndex()
{
/** @var SSE $sse */
$sse = \Yii::$app->sse;
$sse->addEventListener('message', new MessageEventHandler());
$sse->start();
}
这是消息处理程序:
class MessageEventHandler extends SSEBase
{
public function check()
{
return true;
}
public function update()
{
return 'New message!';
}
}
浏览器端:
var sseObject = $.SSE('/notifier', {
events: {
chat_message: function (e) {
console.log(e.data);
}
}
});
sseObject.start();
问题可能与会话锁定有关。
PHP writes its session data to a file by default. When a request is made to a PHP script that starts the session (session_start()), this session file is locked. What this means is that if your web page makes numerous requests to PHP scripts, for instance, for loading content via Ajax, each request could be locking the session and preventing the other requests from completing.
结合 SSE 连接是持久的,因此会话将保持锁定状态。
http://konrness.com/php5/how-to-prevent-blocking-php-requests/
可能的解决方案是在 actionIndex()
中调用 session_write_close()
。