简体   繁体   English

服务器端事件(SSE)未到达客户端

[英]Server Side Events (SSE) not reaching client side

I'm using MEAN and I'm trying to receive events from server side. 我正在使用MEAN,我正在尝试从服务器端接收事件。 For that, Im using EventSource but It just doesn't work. 为此,我使用EventSource,但它不起作用。

I see how connection is open but I don't get any message from server. 我看到连接是如何打开的,但是我没有收到来自服务器的任何消息。 I can see in Node console how the messages are being sent but in client side there's nothing (browser console). 我可以在Node控制台中看到消息是如何发送的,但在客户端则没有(浏览器控制台)。

I'm a bit lost as I'm following all the tutorials I've found, but using exactly the same code it just doesn't work. 我有点迷失,因为我正在按照我发现的所有教程,但使用完全相同的代码它只是不起作用。

In client side, this is my AngularJS code: 在客户端,这是我的AngularJS代码:

  var source = new EventSource('/api/payments/listen');

  source.addEventListener('open', function(e) {
    console.log('CONNECTION ESTABLISHED');
  }, false);

  source.addEventListener('message', function (e) {

    $scope.$apply(function () {
      console.log('NOTIFICATION');
      console.log(e.data);
    });

  }, false);

  source.onmessage = function (e) {
    console.log('NOTIFICATION!');
    console.log(e);
  };

  source.addEventListener('error', function(e) {
    if (e.readyState === EventSource.CLOSED) {
      console.log('CONNECTION CLOSED');
    }
  }, false);

Server side code: 服务器端代码:

exports.listen = function (req, res) {

  if (req.headers.accept && req.headers.accept === 'text/event-stream') {

    if ( req.url === '/api/payments/listen' ) {
      sendSSE(req, res);
    }
    else {
     res.writeHead(404);
     res.end();
    }

  }
  else
    res.end();

};

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, data);
  }, 5000);

  constructSSE(res, id, data);
}

function constructSSE(res, id, data) {

  res.write('id: ' + id + '\n');
  res.write('data: ' + JSON.stringify(data) + '\n\n');

}

Any suggestion? 有什么建议吗? Any tip? 有提示吗?

Edit 编辑

I don't know what was causing the issue, but I have got it to work using Simple-SSE, a useful and little library for server side events. 我不知道是什么导致了这个问题,但我已经使用Simple-SSE工作,这是一个有用的小库,用于服务器端事件。

Now, it is working as expected. 现在,它按预期工作。

Here it is the link for those who wants to give it a try: https://github.com/Lesterpig/simple-sse 在这里,它是那些想要尝试的人的链接: https//github.com/Lesterpig/simple-sse

Thanks =) 谢谢=)

Hope this helps anyone that was having the same issue I was. 希望这可以帮助那些遇到同样问题的人。 I came to this question, after not being able to receive messages from the server although everything in my code seemed to be exactly how it was supposed to. 在我无法接收来自服务器的消息之后,我遇到了这个问题,尽管我的代码中的所有内容似乎都是应该的。 After seeing that this library helped I dug into source code and found my answer. 看到这个库帮助我挖掘源代码并找到答案。 If you are using any compression library such as I was in my express app, you need to flush the response after the write. 如果您正在使用任何压缩库,例如我在我的快速应用程序中,您需要在写入后刷新响应。 other wise the compression doesn't send the message as the protocol expects. 另外,压缩不会像协议所期望的那样发送消息。 eg res.write("data:hi\\n\\n") res.flush() 例如res.write(“data:hi \\ n \\ n”)res.flush()

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

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