服务器发送事件; `EventSource.onmessage` 没有开火

Server Sent Event; `EventSource.onmessage` not firing

我正在尝试使用服务器发送事件的 following example。似乎服务器正在发出数据,但事件没有触发,事件流中没有记录任何数据(通过开发人员工具)。

Angular代码(服务):

getAlertsEvent(fieldIds: Guid[]): Observable<responseModel.LoanAlert> {
    return new Observable(obs => {
      fieldIds.forEach(fieldId => {
        const source = new EventSource(`http://localhost:3000/loan/alerts/${fieldId}`);
        alert('Succesfully creates the EventSource, I see reuslt 200 in Networking tab but with 0 events');
        source.onmessage = (ev): void => {
          this.zone.run(() => {
              alert('This alert will not happen');
              obs.next(ev.data);
            });
          };
        source.onerror = (err): void => this.zone.run(() => obs.error(err));
        // Also tried this way with no luck:
        // source.addEventListener('message', (event) => {
        //   obs.next(event.data);
        // });
      });
    });
  }

组件:

      this.loansService.getAlertsEvent(this.fields.map(field => field.fieldId)).subscribe(alert => {
        console.log(alert);
      });

Node.js代码:

const express = require('express');
const parser = require('body-parser');
const app = express();
const EventEmitter = require('events');

const Stream = new EventEmitter();

app.unsubscribe(parser.json());
app.use(
    parser.urlencoded({
        extended: true,
    })
);

app.get('/loan/alerts/:fieldId', function(req, res) {
    res.writeHead(200, {
        'Content-Type': 'text/event-stream',
        'Cache-Control': 'no-cache',
        'Access-Control-Allow-Origin': "*",
        Conection: 'keep-alive'
    });

    Stream.on(req.params.fieldId, function(event, data) {
        res.write(JSON.stringify(data));
    });
});

setInterval(function() {
    const item = {
        formId: 51415,
        alertId: 'asdfasdfasdf',
        messageStatus: 'NEW',
        messageType: 'ACTION_MESSAGE_FROM_SERVER',
        actionType: 'NAVIGATION',
        message: 'New Message!',
        archiverId: '12345',
        documentCode: 3,
        from: 'Internal Server Message',
        messageTimestamp: new Date().toJSON(),
        markedAsRead: false,
      };
    Stream.emit('aed29580-09fd-e411-b8e1-e61f13cf5d4b', 'message', item);
}, 5000);

app.listen(3000);

console.log('Express E2e Mock server is running');

当手动转到 http://localhost:3000/loan/alerts/aed29580-09fd-e411-b8e1-e61f13cf5d4b 时,我看到屏幕上显示了消息,所以我猜这是 Angular 或缺少安全性header。 谢谢!

我刚刚意识到,感谢 回答,事件必须以特定方式格式化。我相应地更改了 res.write 的值:

Stream.on(req.params.fieldId, function(event, data) {
    res.write('event: ' + String(event) + '\n' + 'data: ' + JSON.stringify(data) + '\n\n');
});