admin管理员组文章数量:1355754
so I'm using server-side events to push messages from the server to the frontend. For front end. I'm using fetch to start the stream, like this
const response = await fetch(`${API_URL}/hello`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': api.defaults.headersmon['Authorization'],
'Accept': 'text/event-stream',
},
body: JSON.stringify(input)
});
if (!response.ok) {
const errorBody = await response.json();
throw new Error(`generateImage HTTP error! status: ${response.status} ${response.statusText}. Details: ${JSON.stringify(errorBody)}`);
}
const reader = response.body.getReader();
const decoder = new TextDecoder();
// everything else the same
How do we close the stream if using fetch? Currently I'm using AbortController but I don't think it's the best practice. Also every time the steam closes, an error will appear on the console log, which is not very nice.
FYI, I used fetch instead of Axios because Axios does not support event stream on Safari and I don't use EventSource either because EventSource only works with GET request, while my server expose a POST request, and I cannot change that.
本文标签: javascriptHow to close stream with serverside event using fetchStack Overflow
版权声明:本文标题:javascript - How to close stream with server-side event using fetch? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744044679a2581306.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论