我想实现一个服务器发送事件,服务器将发送客户端新消息。如何在没有警告/错误的循环中使用session_start()?
这里的挑战是服务器更新会话,然后回显他们的内容。
问题是我需要在一个无限循环中有这个问题。
这是我所做的。
while(true){
session_start();
header("Content-Type: text/event-stream" . PHP_EOL);
header("Cache-Control: no-cache" . PHP_EOL);
//configure the connection
$conf = new ICWS\Config\Config($url, $stationName);
//create a new instance of icws
$attrebutes = array('AccoRDI_mid','AccoRDI_account_id');
$icws = new ICWS\Connection($conf, $attrebutes, true);
$messaging = new ICWS\Messaging($icws);
$messaging->processMessages();
session_write_close();
$result = $messaging->getCallsQueue();
echo 'event: getMessagingQueue' . PHP_EOL;
echo 'data: ' . json_encode($result) . PHP_EOL;
ob_end_flush();
flush();
usleep($sleepTime * 1000000);
}
这里的问题是,我得到以下警告/通知
Warning: session_start(): Cannot send session cache limiter - headers already sent and
Notice: ob_end_flush(): failed to delete and flush buffer. No buffer to delete or flush i
我需要的session_start()
在循环的原因是因为我需要看完后,立即解开了会议文件在脚本睡觉之前。 的想法是
- 开始/恢复会话
- 更新会话值
- 显示会话的内容
- 解锁/释放会话,允许其他进程使用它
- 返回到第1步
我该怎么做才能避免这里的错误?
你不能在5秒内使用cron任务吗? –
“我如何在没有警告/错误的循环中使用session_start()?”你不能 –
我需要实现服务器发送的事件这里是关于我在做什么的更多信息http://stackoverflow.com/questions/30873956/server-sent-events-polling-causing-long-delays – Jaylen