我最近发现了EventSource,YUI3有一个Gallery模块来规范和回退行为,这是我在我的例子中选择的,因为我已经使用了这个框架。
I've recently discovered EventSource, YUI3 has a Gallery module to normalise and fallback behaviour, that's what I've chosen to go with in my example as I use that framework already.
所以我搜索了很多,阅读了很多博客,帖子和例子,所有这些都显示了几乎相同的事情:如何设置基本的SSE事件。我现在有6个打开/消息/错误/关闭事件的例子。
So I've searched about quite a bit, read many blogs, posts and examples, all of which show pretty much the same thing: How to set up basic SSE events. I now have 6 examples of open/message/error/close events firing.
我没有(我希望此链接将给予我)是一个例子,说明如何发起对我的应用程序更有用的SSE事件,我正在尝试一个名为'update'。
What I don't have (what I'd hoped this link was going to give me) is an example of how to fire SSE events which are more useful to my application, I'm trying one called 'update'.
这是我的基本测试页面: codefinger.co.nz/public/yui/eventsource/test.php (它也可能是一个html文件,没有php代码在这里)
Here's is my basic test page: codefinger.co.nz/public/yui/eventsource/test.php (it might as well be an html file, there's no php code in here yet)
这里是EventSource构造函数中的message.php:
And here's the 'message.php' in the EventSource constructor:
<?php header('Content-Type: text/event-stream'); header('Cache-Control: no-cache'); // recommended to prevent caching of event data. /** * Constructs the SSE data format and flushes that data to the client. * * @param string $id Timestamp/id of this connection. * @param string $msg Line of text that should be transmitted. */ function sendMsg($id, $msg) { echo "id: $id" . PHP_EOL; echo "data: $msg" . PHP_EOL; echo PHP_EOL; ob_flush(); flush(); } while(true) { $serverTime = time(); sendMsg($serverTime, 'server time: ' . date("h:i:s", time())); sleep(10); } // I was hoping calling this file with a param might allow me to fire an event, // which it does dutifully, but no browsers register the 'data : update' - though // I do see the response in Firebug. if( $_REQUEST['cmd'] ){ sendMsg($serverTime, $_REQUEST['cmd'] ); } ?>从上面的实例,您可以看到我已经尝试使用YUI的io模块发送当我点击更新按钮时,请求与param一起触发我的更新事件。好像在Firebug的Net面板中可以看到它,但是我的事件没有被处理(我认识到上面的脚本会再次运行这个循环,我只想在连接的浏览器中处理我的事件,然后我将删除/ cleanup)。
From the live example above, you can see that I've tried to use YUI's io module to send a request, with param, to fire my 'update' event when I click the 'update' button. It seems to work, as you can see in Firebug's Net panel, but my event isn't handled (I realise the script above will run that loop again, I just want to get my event handled in connected browsers, then I'll remove/cleanup).
我做这个部分错了吗?还是有更重要的东西我做错了?我试图推动事件响应我的UI的状态改变。
Am I doing this part wrong? Or is there something more fundamental I'm doing wrong? I'm trying to push events in response to my UI's state changing.
这个SO问题似乎很接近,@tomfumb评论说他的下一个问题将是如何向客户端发送新的事件在初始化连接之后 - 现在我看到PHP只是不得不停止执行。但是当然,我只会发送事件,而不是连续发送...
This SO question seemed to come close, @tomfumb commented that his next question was going to be "how to send new events to the client after the initial connection is made - now I see that the PHP just has to never stop executing." But surely I'd only send events as they happen... and not continuously...
推荐答案有几个问题您的方法:
如果您选择继续使用SSE API,我会看到两种可能的情况:
If you choose to stay with the SSE API I see two possible scenarios
此外,您可以使用XMLHttpRequest(long)轮询,从而避免使用EventSource。由于您的示例的简单性,我无法看到混合两种请求的原因。
Also you can use XMLHttpRequest with (long) polling and thus avoiding the need of using EventSource. Because of the simplicity of your example I can't see a reason to mix the two type of requests.
更多推荐
如何发起EventSource SSE活动?
发布评论