gpt4 book ai didi

javascript - 如何订阅事件流 - 服务器端

转载 作者:太空宇宙 更新时间:2023-11-04 00:37:25 27 4
gpt4 key购买 nike

我有一个返回 text/event-stream 数据的网址,因此我尝试连接并打印我找到的所有内容:

var url = "..."
var source = new EventSource(url);

source.addEventListener('message', function(e) {
console.log(e.data);
}, false);

source.addEventListener('open', function(e) { }, false);

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

不幸的是,这在服务器端不起作用。我可以这样连接吗?

$ node app.js

app.js:2
var source = new EventSource(url);
^

ReferenceError: EventSource is not defined
at Object.<anonymous> (app.js:2:18)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3

最佳答案

看来你忘记了服务器端:

var EventSource = require("eventsource");

关于javascript - 如何订阅事件流 - 服务器端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38387395/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com