模块 ringo/jsgi/eventsource

此模块为 EventSource 响应对象提供构造函数,该对象允许将消息推送到连接的客户端。

Functions

Class EventSource

Instance Methods

Instance Properties


EventSource (request)

EventSource(或Server-Sent-Events)是一种利用普通 HTTP 响应的服务器推送技术。事件流格式定义了三种类型的消息:

  • data-only
  • named events with data
  • comments

每种消息类型都有一种方法可用。数据预计采用 JSON 格式。 EventSource 实例是线程安全的。

EventSource 包装一个 AsyncResponse 并且使用类似:

Example

var eventSource = new EventSource(request);
   // send headers and start heartbeat
   eventSource.start({
      "X-Additional-Header": "Foo"
   });
   setInterval(function() {
       eventSource.event('foo-field', 'foo-value');
   }, 5 * 1000);

   // close the response. No more data can be written
   // and the hearbeat stops.
   eventSource.close();

   // Each EventSource instance exposes the wrapped JSGI asynchronous response
   eventSource.response

Parameters

JSGIRequest request

EventSource.prototype. close

关闭事件源。

Throws

{Error}

EventSource.prototype. comment (comment)

发送评论。

Parameters

String comment

The comment

Throws

{Error}

EventSource.prototype. data (data)

发送数据

Parameters

String data

The event data

Throws

{Error}

EventSource.prototype. event (name, data)

发送命名事件

Parameters

String name

The event name

String data

The event data

Throws

{Error}

EventSource.prototype. ping

发送 ping 到客户端

Throws

{Error}

EventSource.prototype. response


EventSource.prototype. start (headers, heartBeatInterval)

可选择在此处设置其他headers.

Parameters

Object headers

Additional headers (optional)

Number heartBeatInterval

in seconds (optional. default: 15)


isEventSourceRequest (request)

静态助手检查请求是否接受事件流。

Parameters

JSGIRequest request

Returns

Boolean

whether the accept header matches 'text/event-stream