fix: fetch无法返回正确从chunk
This commit is contained in:
parent
9a11937a6b
commit
108086990c
@ -273,6 +273,7 @@ function chatMessage() {
|
||||
ChatManagement.addChatRecord(row, 50, loading)
|
||||
ChatManagement.write(id)
|
||||
const reader = response.body.getReader()
|
||||
let tempResult = ''
|
||||
/*eslint no-constant-condition: ["error", { "checkLoops": false }]*/
|
||||
const write = ({ done, value }: { done: boolean; value: any }) => {
|
||||
try {
|
||||
@ -282,7 +283,16 @@ function chatMessage() {
|
||||
}
|
||||
|
||||
const decoder = new TextDecoder('utf-8')
|
||||
const str = decoder.decode(value, { stream: true })
|
||||
let str = decoder.decode(value, { stream: true })
|
||||
// 这里解释一下 start 因为数据流返回流并不是按照后端chunk返回 我们希望得到的chunk是data:{xxx}\n\n 但是它获取到的可能是 data:{ -> xxx}\n\n 总而言之就是 fetch不能保证每个chunk都说以data:开始 \n\n结束
|
||||
tempResult += str
|
||||
if (tempResult.endsWith('\n\n')) {
|
||||
str = tempResult
|
||||
tempResult = ''
|
||||
} else {
|
||||
return reader.read().then(write)
|
||||
}
|
||||
// 这里解释一下 end
|
||||
if (str && str.startsWith('data:')) {
|
||||
const split = str.match(/data:.*}\n\n/g)
|
||||
if (split) {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user