Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net::ERR_HTTP2_PROTOCOL_ERROR 200 (OK) and abortController don't work #78

Open
YakumoAoi opened this issue Mar 18, 2024 · 4 comments
Open

Comments

@YakumoAoi
Copy link

const abortController = new AbortController()
  await fetchEventSource(
    `https://xxx/xx/xx/xx?query=${questionSended}&dialog_id=${this.currentDialogID}&is_stream=1`,
    {
      headers: {
        Authorization: userinfo.token
      },
      signal: abortController.signal,
      openWhenHidden: true,
      onopen: async response => {
        console.log('onopen', response)
        if (response.status !== 200) {
          throw new Error(`${response.status}(${response.statusText})`)
        }
        return
      },
      onmessage: res => {
        const data = JSON.parse(res.data)
        console.log(data)
        if (data.plugin_id) return
        if (data.error_code) {
          throw new Error(`${data.error_code}(${data.error_msg})`)
        }
        if (data.prompt) {
          this.relatedQuestion = data.prompt
        } else {
     
        }
      },
      onclose: () => {
        console.log('closed')
      },
      onerror: err => {
        console.log('err', err)
        if (abortController.signal.aborted) return
        this.dialogDetailList[contextIndex].answer =
          'error'
        abortController.abort()
      }
    }
  )

The error occured in the middle of request, and the browser start another request to continue receiving stream data. And the same error occured. such back and forth
And l can read true value of 'abortController.signal.aborted' in error callback, but the request still continue

@lujdong
Copy link

lujdong commented Mar 19, 2024

same error

@Cai-Quan
Copy link

Me too. Have you solved the problem?

@mqyqingfeng
Copy link

'use client';

import { fetchEventSource } from '@microsoft/fetch-event-source';
import { useEffect, useState } from "react";

export default function Home() {

  const [text, setText] = useState('')

  useEffect(() => {
    const ctrl = new AbortController();
    fetchEventSource('/api/sse4', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({
        foo: 'bar'
      }),
      signal: ctrl.signal,
      onmessage: function (event) {
        console.log('Received message:', event.data)
        setText((pre) => pre + event.data);
      }
    });

    return () => {
      ctrl.abort()
    }
  }, [])

  return (
    <p>{text}</p>
  );
}

@wooly99
Copy link

wooly99 commented Jun 17, 2024

#46
#24

It seems that you need to throw an error in the onerror callback

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants