1
A
回答
0
我使用超薄API版本2和我添加的服务器发送的事件,以我现有的API是这样的:
在index.php文件:
$app = new \Slim\Slim();
// other code
....
// this is the route I want to use for the event stream
$app->get('/psoback/eventstream',
function() use ($app)
{
require_once('event.php');
$app->eventstream = new ServerSentEventHandler();
}
);
// here goes the rest of my api definitions
...
$app->run();
if(isset($app->eventstream))
{
$app->eventstream->Run();
}
event.php看起来是这样的: (例如从https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)
<?php
class ServerSentEventHandler
{
function __construct()
{
}
function Run()
{
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$counter = rand(1, 10);
while (1)
{
// Every second, sent a "ping" event.
echo "event: ping\n";
$curDate = date(DATE_ISO8601);
echo 'data: {"time": "' . $curDate . '"}';
echo "\n\n";
// Send a simple message at random intervals.
$counter--;
if (!$counter)
{
echo 'data: This is a message at time ' . $curDate . "\n\n";
$counter = rand(1, 10);
}
ob_end_flush();
flush();
sleep(1);
}
}
};
?>
相关问题
- 1. HTML5服务器使用JSON和PHP发送的事件(SSE)
- 2. 服务器发送事件(SSE)和科尔多瓦/ Phonegap
- 3. Google App Engine中的服务器发送事件(SSE)
- 4. WebSocket与SSE之间的MQTT差异(服务器发送事件)
- 5. 如何在android中使用服务器发送的事件(SSE)?
- 6. 服务器发送的事件不适用于运动衫SSE
- 7. 使用servlet作为服务器进行服务器发送的事件(SSE)
- 8. Dropwizard和Jersey SSE(服务器端事件)
- 9. 服务器发送事件 - 事件流 - 触发PHP服务器端事件?
- 10. 使用服务器发送的事件(SSE),PHP没有达到max_execution_time?
- 11. 服务器用PHP发送事件
- 12. SSE未捕获错误:SECURITY_ERR:DOM异常18与提供服务器发送事件(SSE)的服务器
- 13. Websocket,服务器发送事件(SSE)和HTTP2的服务器推送之间有什么区别?
- 14. 如何使服务器发送事件(SSE)与PHP会话一起工作?
- 15. 服务器发送事件
- 16. SSE客户端随时停止工作我修改服务器(服务器发送事件)
- 17. PHP:发送服务器发送到特定IP的DOM事件
- 18. PHP服务器发送的事件 - 消息发送方和收件箱
- 19. PHP gzip和服务器发送的事件流
- 20. 服务器发送的事件和php - 如何成为代理
- 21. 使用服务器发送的事件和PHP会话
- 22. 服务器端事件不使用SSE
- 23. 微服务,服务器发送事件和浏览器限制
- 24. 移动电话上的服务器发送事件(SSE)连接丢失
- 25. 在公司环境中使用SSE(服务器发送的事件)
- 26. 涉及服务器发送的事件
- 27. 检查服务器发送的事件
- 28. Node.JS服务器发送的事件
- 29. 服务器发送的事件
- 30. SSE(服务器发送事件),监听Node.js服务器中的事件并将其发布到其他服务器