由微软开源的https://github.com/Azure/fetch-event-source鸿蒙化
利用系统的http库实现,提供了更灵活、功能更全面的事件源请求接口。这个库兼容Event Stream格式,使您能轻松地与现有的SSE服务器进行交互,并拥有更多的控制权和定制性。
通过 ohpm
ohpm install fetch_event_sourceimport { fetchEventSource } from "fetch_event_source"
await fetchEventSource('/api/sse', {
onmessage(ev) {
console.log(ev.data);
}
});复杂一些的使用:
import { fetchEventSource } from "fetch_event_source"
fetchEventSource('/api/sse', {
method: http.RequestMethod.POST,
header: Object({
'Content-Type': 'application/json',
'Authorization': 'Bearer sk-xxx'
}),
extraData: JSON.stringify({
foo: 'bar'
}),
onopen: async () => {
},
onmessage: (ev) => {
console.log(ev.data);
},
onclose: () => {
},
onerror: (error) => {
console.error('Error:', error)
return undefined
}
})