服务器发送事件; `EventSource.onmessage`没有触发
Posted
技术标签:
【中文标题】服务器发送事件; `EventSource.onmessage`没有触发【英文标题】:Server Sent Event; `EventSource.onmessage` not firing 【发布时间】:2022-01-22 16:59:55 【问题描述】:我正在尝试使用服务器发送事件的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 或缺少安全标头。 谢谢!
【问题讨论】:
【参考方案1】:我刚刚意识到,感谢this 的回答,事件必须以特定方式格式化。我相应地更改了res.write
的值:
Stream.on(req.params.fieldId, function(event, data)
res.write('event: ' + String(event) + '\n' + 'data: ' + JSON.stringify(data) + '\n\n');
);
【讨论】:
以上是关于服务器发送事件; `EventSource.onmessage`没有触发的主要内容,如果未能解决你的问题,请参考以下文章