如何发起EventSource SSE活动? [英] How to fire EventSource SSE events?

查看:227
本文介绍了如何发起EventSource SSE活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发现了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'.

这是我的基本测试页面: http://codefinger.co.nz/public/yui/eventsource/test.php (它也可能是一个html文件,没有php代码在这里)

Here's is my basic test page: http://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...

推荐答案

有几个问题您的方法:



  1. 读取cmd参数的服务器端代码无法访问,因为发送事件数据的无限循环给客户

  2. 您正在尝试将事件从客户端发送到服务器。它在规范名称 - 服务器发送事件 - 服务器是发件人,客户端是事件的接收者。您可以在这里选择:

  1. The server-side code that reads the cmd parameter is unreachable because of the infinite loop that sends event data to the client.
  2. You are trying to send an event from the client to the server. It is in the specification name - Server-Sent Events - the server is the sender and the client is the receiver of events. You have options here:


  1. 对于名为 Web Sockets 是一个双向通信API

  2. 编写使所需类型的通信成为可能的逻辑

  1. Use the appropriate specification for the job called Web Sockets which is a two-way communication API
  2. Write the logic that makes the desired type of communication possible


如果您选择继续使用SSE API,我会看到两种可能的情况:

If you choose to stay with the SSE API I see two possible scenarios



  1. 重新使用相同的事件源连接,并在服务器上存储连接池。当用户使用update命令发送后续的XMLHttpRequest时,从该访问者所创建的池中获取EventSource连接,并发送指定自定义事件类型的响应,默认类型为消息。重要的是避免进入无限循环,这将使另一个EventSource连接到客户端,但客户端不处理它,因为他使用XMLHttpRequest发出请求,而不是使用EventSource。

  2. 使用EventSource进行所有请求。在创建新的EventSource请求之前,关闭前一个请求 - 您可以从客户端或从服务器执行此操作。在服务器上检查参数,然后将数据发送给客户端。
  1. Reuse the same Event Source connection and store a pool of connections on the server. When the user sends subsequent XMLHttpRequest with the update command, get the EventSource connection from the pool, that was made by this visitor, and send response with it that specifies your custom event type, the default type is message. It is important to avoid entering in the infinite loop that would make another EventSource connection to the client, but the client does not handle it because he made the request with XMLHttpRequest and not with EventSource.
  2. Make all requests with EventSource. Before making a new EventSource request, close the previous one - you can do this from the client or from the server. On the server check the parameters and then send data to client.

此外,您可以使用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活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆