简体   繁体   English

html5 rock节点js server-sent-events SSE示例不起作用

[英]html5 rocks node js server-sent-events SSE example not working

link to article: http://www.html5rocks.com/en/tutorials/eventsource/basics/ 链接到文章: http//www.html5rocks.com/en/tutorials/eventsource/basics/

The node.js SSE server is not working in that example. node.js SSE服务器在该示例中不起作用。 I end up with an open connection to /events , but no response is received by the browser. 我最终打开了与/events连接,但浏览器没有收到任何响应。

Chrome开发工具prnt scrn

sse-server.js SSE-server.js

var http = require('http');
var sys = require('sys');
var fs = require('fs');

http.createServer(function(req, res) {
  //debugHeaders(req);

  if (req.headers.accept && req.headers.accept == 'text/event-stream') {
    if (req.url == '/events') {
      sendSSE(req, res);
    } else {
      res.writeHead(404);
      res.end();
    }
  } else {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write(fs.readFileSync(__dirname + '/sse-node.html'));
    res.end();
  }
}).listen(8000);

function sendSSE(req, res) {
  res.writeHead(200, {
    'Content-Type': 'text/event-stream',
    'Cache-Control': 'no-cache',
    'Connection': 'keep-alive'
  });

  var id = (new Date()).toLocaleTimeString();

  // Sends a SSE every 5 seconds on a single connection.
  setInterval(function() {
    constructSSE(res, id, (new Date()).toLocaleTimeString());
  }, 5000);

  constructSSE(res, id, (new Date()).toLocaleTimeString());
}

function constructSSE(res, id, data) {
  res.write('id: ' + id + '\n');
  res.write("data: " + data + '\n\n');
}

function debugHeaders(req) {
  sys.puts('URL: ' + req.url);
  for (var key in req.headers) {
    sys.puts(key + ': ' + req.headers[key]);
  }
  sys.puts('\n\n');
}

sse-node.html SSE-node.html

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8" />
</head>
<body>
  <script>
    var source = new EventSource('/events');
    source.onmessage = function(e) {
      document.body.innerHTML += e.data + '<br>';
    };
  </script>
</body>
</html>

The problem was with the server. 问题出在服务器上。 In my case I was using node with IIS using the iisnode node package. 在我的情况下,我使用iisnode节点包使用IIS节点。 To solve this, I needed to configure iisnode in the web.config like so: 为了解决这个问题,我需要在web.config中配置iisnode,如下所示:

<iisnode flushResponse="true" />

After this, everything worked fine. 在此之后,一切正常。 Others with a similar issue may start here, but apache and nginx may have similar configuration requirements. 其他有类似问题的人可能会从这里开始,但是apache和nginx可能有类似的配置要求。

Why did you comment out the res.end() at the end of the sendSSE function? 你为什么在sendSSE函数的末尾注释掉res.end()? That's the method that actually sends the response to the browser. 这是实际将响应发送到浏览器的方法。

我已经尝试过该代码并且正在为我工​​作,因为您没有使用ngnix或任何其他服务器作为您的节点实例的代理我会认为问题出在您的机器上,如果您有防火墙或防病毒运行,请停止它,再试一次或任何其他可能拦截你的req和res的软件。

Make sure you have saved the HTML file with same name as described sse-node.html in same directory. 确保已在同一目录中保存了与sse-node.html中描述的名称相同的HTML文件。 Other thing might be make sure 8000 port is open in your local machine no one is using it. 其他可能是确保在本地计算机上打开8000端口没有人使用它。 or change the port and re-run sse-server.js. 或更改端口并重新运行sse-server.js。 its working for me as is. 它按原样为我工作。

I faced the same issue. 我遇到了同样的问题。 Nothing wrong with your code just Provide Full Resource Address in your HTML File. 您的代码没有任何问题只需在HTML文件中提供完整资源地址

var source = new EventSource('http://localhost:8000/events'); //true way

instead of 代替

var source = new EventSource('/events'); //improper way

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM