服务器端事件(SSE)未到达客户端 [英] Server Side Events (SSE) not reaching client side

查看:47
本文介绍了服务器端事件(SSE)未到达客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MEAN,并且试图从服务器端接收事件.为此,我使用EventSource,但是它不起作用.

I'm using MEAN and I'm trying to receive events from server side. For that, Im using EventSource but It just doesn't work.

我看到连接是如何打开的,但是没有从服务器收到任何消息.我可以在Node控制台中看到消息的发送方式,但是在客户端却什么也没有(浏览器控制台).

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).

当我按照我发现的所有教程进行操作时,我有点迷失了,但是使用完全相同的代码却是行不通的.

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.

在客户端,这是我的AngularJS代码:

In client side, this is my AngularJS code:

  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);

服务器端代码:

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?

修改

我不知道是什么原因引起的,但是我已经使用Simple-SSE来使它起作用,Simple-SSE是服务器端事件的有用且很小的库.

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.

现在,它正在按预期方式工作.

Now, it is working as expected.

这是想要尝试的人的链接: https://github.com/Lesterpig/simple-sse

Here it is the link for those who wants to give it a try: https://github.com/Lesterpig/simple-sse

谢谢=)

推荐答案

希望这对遇到同样问题的人有所帮助.在无法从服务器接收消息之后,尽管我代码中的所有内容似乎都是应该的,但我还是遇到了这个问题.看到这个库帮助我研究了源代码并找到了答案.如果使用的压缩库(例如我在Express应用程序中使用的压缩库),则需要在写入后刷新响应.否则,压缩不会像协议期望的那样发送消息.例如res.write("data:hi \ n \ n")res.flush()

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. e.g. res.write("data:hi\n\n") res.flush()

这篇关于服务器端事件(SSE)未到达客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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