676ec2da14 前端页面完成 %!s(int64=4) %!d(string=hai) anos
..
lib 676ec2da14 前端页面完成 %!s(int64=4) %!d(string=hai) anos
test 676ec2da14 前端页面完成 %!s(int64=4) %!d(string=hai) anos
.npmignore 676ec2da14 前端页面完成 %!s(int64=4) %!d(string=hai) anos
.travis.yml 676ec2da14 前端页面完成 %!s(int64=4) %!d(string=hai) anos
CONTRIBUTING.md 676ec2da14 前端页面完成 %!s(int64=4) %!d(string=hai) anos
History.md 676ec2da14 前端页面完成 %!s(int64=4) %!d(string=hai) anos
LICENSE 676ec2da14 前端页面完成 %!s(int64=4) %!d(string=hai) anos
README.md 676ec2da14 前端页面完成 %!s(int64=4) %!d(string=hai) anos
example.js 676ec2da14 前端页面完成 %!s(int64=4) %!d(string=hai) anos
package.json 676ec2da14 前端页面完成 %!s(int64=4) %!d(string=hai) anos

README.md

EventSource Build Status Dependencies Bitdeli Badge

NPM NPM

This library implements the EventSource client for Node.js. The API aims to be W3C compatible.

Install

npm install eventsource

Usage

var EventSource = require('eventsource');

var es = new EventSource('http://demo-eventsource.rhcloud.com/');
es.onmessage = function(e) {
  console.log(e.data);
};
es.onerror = function() {
  console.log('ERROR!');
};

See the spec for API docs.

Example

See https://github.com/einaros/sse-example

Extensions to the W3C API

Setting HTTP request headers

You can define custom HTTP headers for the initial HTTP request. This can be useful for e.g. sending cookies or to specify an initial Last-Event-ID value.

HTTP headers are defined by assigning a headers attribute to the optional eventSourceInitDict argument:

var eventSourceInitDict = {headers: {'Cookie': 'test=test'}};
var es = new EventSource(url, eventSourceInitDict);

Allow unauthorized HTTPS requests

By default, https requests that cannot be authorized will cause connection to fail and an exception to be emitted. You can override this behaviour:

var eventSourceInitDict = {rejectUnauthorized: false};
var es = new EventSource(url, eventSourceInitDict);

Note that for Node.js < v0.10.x this option has no effect - unauthorized HTTPS requests are always allowed.

HTTP status code on error events

Unauthorized and redirect error status codes (for example 401, 403, 301, 307) are available in the status property in the error event.

es.onerror = function (err) {
  if (err) {
    if (err.status === 401 || err.status === 403) {
      console.log('not authorized');
    }
  }
};