为什么EventSourcePolyfill报错信息没有输出任何信息

问题描述 投票:0回答:0

我已经定义了 EventSourcePolyfill

"event-source-polyfill": "^1.0.31"
客户端来接收这样的 sse 消息:

import { IChatAsk } from '../../models/chat/ChatAsk';
import { EventSourcePolyfill } from 'event-source-polyfill';
import { v4 as uuid } from 'uuid';

export function doSseChatAsk(params: IChatAsk, onSseMessage: (msg: string) => void,) {
  let eventSource: EventSourcePolyfill;
  const accessToken = localStorage.getItem("x-access-token");
  eventSource = new EventSourcePolyfill('/ai/stream/chat/ask?question=hello', {
    headers: {
      'x-access-token': accessToken ?? "",
      'x-request-id': uuid(),
    }
  });
  eventSource.onopen = () => {
    console.log("onopen....")
  }
  eventSource.onerror = (error) => {
    console.log("onerror",error)
    if(eventSource){
      eventSource.close();
    }
  }
  eventSource.onmessage = e => {
    onSseMessage(e.data);
  };

  eventSource.addEventListener('complete', () => {
    console.log('Transfer of data is complete');
  });
  
}

这段代码遇到onerror代码块时,没有输出任何有效信息。输出信息是这样的:

我应该怎么做才能从上下文中获取有效的错误消息?我已经使用 curl 命令像这样测试服务器端:

➜  ~ curl -X GET -H 'Content-Type: application/json' -H 'x-request-id:1' -H 'Cache-Control:no-cache'  -H 'x-access-token: eyJhbGciOiJIJ9.eyJZTDkiLC2Nzk.G9Ddi5sBMmiKNaD_vKni-gzN5kdT6426ruo1EDDV29SCFwI0CqlS5hKg6D7Q' -N https://ai.example.top/ai/stream/chat/ask\?question\=1

data:{"id": "cmpl-6xtZ5KyFfDfa97NJrGHqq2DdsXJQE", "object": "text_completion", "created": 1679732963, "choices": [{"text": " world", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}

data:{"id": "cmpl-6xtZ5KyFfDfa97NJrGHqq2DdsXJQE", "object": "text_completion", "created": 1679732963, "choices": [{"text": "\n", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}

data:{"id": "cmpl-6xtZ5KyFfDfa97NJrGHqq2DdsXJQE", "object": "text_completion", "created": 1679732963, "choices": [{"text": "\n", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}

data:{"id": "cmpl-6xtZ5KyFfDfa97NJrGHqq2DdsXJQE", "object": "text_completion", "created": 1679732963, "choices": [{"text": "Hello", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}

data:{"id": "cmpl-6xtZ5KyFfDfa97NJrGHqq2DdsXJQE", "object": "text_completion", "created": 1679732963, "choices": [{"text": " world", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}

data:{"id": "cmpl-6xtZ5KyFfDfa97NJrGHqq2DdsXJQE", "object": "text_completion", "created": 1679732963, "choices": [{"text": "!", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}

data:{"id": "cmpl-6xtZ5KyFfDfa97NJrGHqq2DdsXJQE", "object": "text_completion", "created": 1679732963, "choices": [{"text": " It", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}

data:{"id": "cmpl-6xtZ5KyFfDfa97NJrGHqq2DdsXJQE", "object": "text_completion", "created": 1679732963, "choices": [{"text": "'s", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}

data:{"id": "cmpl-6xtZ5KyFfDfa97NJrGHqq2DdsXJQE", "object": "text_completion", "created": 1679732963, "choices": [{"text": " nice", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}

data:{"id": "cmpl-6xtZ5KyFfDfa97NJrGHqq2DdsXJQE", "object": "text_completion", "created": 1679732963, "choices": [{"text": " to", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}

data:{"id": "cmpl-6xtZ5KyFfDfa97NJrGHqq2DdsXJQE", "object": "text_completion", "created": 1679732963, "choices": [{"text": " meet", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}

data:{"id": "cmpl-6xtZ5KyFfDfa97NJrGHqq2DdsXJQE", "object": "text_completion", "created": 1679732963, "choices": [{"text": " you", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}

data:{"id": "cmpl-6xtZ5KyFfDfa97NJrGHqq2DdsXJQE", "object": "text_completion", "created": 1679732963, "choices": [{"text": ".", "index": 0, "logprobs": null, "finish_reason": null}], "model": "text-davinci-003"}

我做了一个最小的复现例子,好像是服务器问题:

<!DOCTYPE html>
<html>
  <head>
    <title>SSE Client</title>
  </head>
  <body>
    <div id="sse"></div>
    <script>
      const sse = new EventSource('https://ai.example.top/ai/stream/chat/ask?question=1');

      sse.onmessage = event => {
        const data = JSON.parse(event.data);
        document.getElementById('sse').innerText = data.message;
      };
      sse.onerror = event => {
        console.log(event);
      }
    </script>
  </body>
</html>
reactjs server-sent-events eventsourcepolyfill
© www.soinside.com 2019 - 2024. All rights reserved.