vue项目如何接收SSL数据流

vue   2025-04-07 14:54   32   0  

1.安装fetch-event-source插件

npm install @microsoft/fetch-event-source

2.使用方法如下

fetchEventSource('https://spark-api-open.xf-yun.com/v1/chat/completions', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json',
                "Authorization": "Bearer apikey",
            },
            body: JSON.stringify(form),
            openWhenHidden: true,
            onopen(ops) {
                // console.log(ops);
            },
            onmessage(res) {
                // eventSource.close();
                if (res.data == '[DONE]') {
                    data.disabledSend = false;
                } else {
                    let resdata = JSON.parse(res.data);
                    textS+= resdata.choices[0].delta.content;
                    data.messages[msglen + 1] = { sender: 'admin', text: textS, msglen };
                    // console.log(data.messages)
                }
            },
            onerror(err) {
                throw Error(err);
            }
});


博客评论
还没有人评论,赶紧抢个沙发~
发表评论
说明:请文明发言,共建和谐网络,您的个人信息不会被公开显示。