vue 小程序实现流式数据接收

vue

安装

npm install @microsoft/fetch-event-source

//链接fetchEventSource

方法

function connectFetchEventSource(param: any) {

  const ctrl = new AbortController()

  const eventSource: any = fetchEventSource(param.api_url, {

    method: 'POST',

    // mode: 'no-cors',

    headers: {

      'Content-Type': 'application/json',

      // Authorrization: 'Bearer sk-q7LIaxAS5pTIofgd82C88dE2B5Db4c979976B5Da9cB15a84'

      Authorization: 'Bearer ' + param.api_sk

    },

    body: JSON.stringify(param),//参数

    signal: ctrl.signal,

    onmessage(ev) {

结果

        let result = JSON.parse(ev.data)

    },

    onclose() {

      // eventSource.abort()

    },

    onerror(error) {

      console.log(error)

      // eventSource.abort()

    }

  })

  console.log('submit!')

}

小程序


//外部js引用时这样写:import {Base64} from '/xxx/base64';
const Base64 = {
    // 转码表
    tables : [
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
            'I', 'J', 'K', 'L', 'M', 'N', 'O' ,'P',
            'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
            'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
            'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
            'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
            'w', 'x', 'y', 'z', '0', '1', '2', '3',
            '4', '5', '6', '7', '8', '9', '+', '/'
    ],
    UTF16ToUTF8 : function (str) {
        let results = [], len = str.length;
        for (let i = 0; i < len; i++) {
            let code = str.charCodeAt(i);
            if (code > 0x0000 && code <= 0x007F) {
                /* 一字节,不考虑0x0000࿰

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值