当前位置: 首页 > news >正文

懂的建设网站推广平台

懂的建设网站,推广平台,网站页面设计要求,百度推广 网站吸引力微信小程序对接SSE接口记录 需求:公司项目对接gpt,gpt产生的结果是分段返回,所以要求在产生结果时,有打字机的效果。原本是由定时器调用,后来优化改为服务端使用SSE接口。小程序使用起来比较方便,但是要求…

微信小程序对接SSE接口记录

  • 需求:公司项目对接gpt,gpt产生的结果是分段返回,所以要求在产生结果时,有打字机的效果。原本是由定时器调用,后来优化改为服务端使用SSE接口。
  • 小程序使用起来比较方便,但是要求小程序基本库的版本需要在2.20.2以上。文档地址移步这里
  • 微信小程序代码
   // 基础库为2.33.0const requestTask = wx.request({url: `xxxxxxxx`, // 需要请求的接口地址enableChunked: true // enableChunked必须为true})// 开发工具存在问题,使用真机测试const listener = data => {// data为返回的数据,可以在此对数据进行处理}// 监听服务端返回的数据requestTask.onChunkReceived(listener)// 移除监听 需传入与监听时同一个的函数对象requestTask.offChunkReceived(listener)

注意:

  1. 接收到的结果数据类型固定为arrayBuffer,需要开发者自己进行转换,可以使用下面方法进行转换。
function arrayBufferToString(arr){if(typeof arr === 'string') {  return arr;  }  var dataview = new DataView(arr);var ints = new Uint8Array(arr.byteLength);for(var i=0;i<ints.length;i++){ints[i]=dataview.getUint8(i);}var str = '',  _arr = ints;  for(var i = 0; i < _arr.length; i++) {if (_arr[i]) {var one = _arr[i].toString(2),  v = one.match(/^1+?(?=0)/);  if(v && one.length == 8) {var bytesLength = v[0].length;var store = _arr[i].toString(2).slice(7 - bytesLength);  for(var st = 1; st < bytesLength; st++) {  if ( _arr[st + i]) {store += _arr[st + i].toString(2).slice(2);  }}  str += String.fromCharCode(parseInt(store, 2));  i += bytesLength - 1;  } else {  str += String.fromCharCode(_arr[i]);  }  }}  return str; 
}
  1. 微信开发工具中无法转换数据。可能是由于开发工具问题,在服务端返回的字符串中存在中文时,开发工具是无法正常转换的。但是在真机是正常的。如果需要在开发工具中实现转换,可以与服务端协商将数据进行URL编码返回。
  2. 服务端一次返回的结果,微信小程序有时会将其截开,并分两次返回。由于截开的位置并不固定,所以可能会存在转换ArrayBuffer时,出现结果异常的问题。使用SSE接口一般有两种需求:一种是将所有的结果累加起来、还有一种就是后面的结果覆盖前面的。在使用第一种时,每次的返回量不会太大,所以应该不会出现微信小程序截开两次返回的情况。但是第二种每次返回的接口都在逐渐增大,可能会出现这种情况,我就是第二种。我是使用下面方法解决
// 我的数据是json字符串,如果出现分开返回,在转json时,会出现报错,所以使用try处理
let timer = null
const listener = data => {try {// 上次结果出现报错 这次正常 清除延时器if (timer) {clearTimeout(timer)timer = null}// 小程序存在数据截开的情况  存五次数据if (arr.length > 4) {arr.shift()}// 这里要存储的是arrayBuffer,不能存储string数据arr.push(data.data)// 数据处理 .......} catch (e) {// 最后一次出现报错 三秒后重组数据timer = setTimeout(() => {const len = arr.lengthlet index = len - 2,data = arr[len - 1],result = nullwhile(index > -1) {// 从后往前 合并data = mergeArrayBuffers(arr[index], data)try{// 数据处理 .......index = -1}catch(e){index -= 1}}}, 3000)}}
// 合并arrayBuffer
function mergeArrayBuffers(buffer1, buffer2) {if (!buffer1) {return buffer2;} else if (!buffer2) {return buffer1;}var tmp = new Uint8Array(buffer1.byteLength + buffer2.byteLength);tmp.set(new Uint8Array(buffer1), 0);tmp.set(new Uint8Array(buffer2), buffer1.byteLength);return tmp.buffer;
}
  • 由于是后面的结果覆盖前面的,我只需要处理最后一次结果,如果结果正常则不用处理。不正常再将前面存储的数据一一合并,再做处理。
  • 通常一个中文是两个字节,所以可能会出现一个中文恰好被截开的情况,所以需要存储的是原数据
  1. 由于SSE特性,需要由用户端断开连接,所以在使用完毕时,需要调用requestTask.abort()断开连接
    ps: 此文章做个人平常记录

文章转载自:
http://dinncoelectrification.stkw.cn
http://dinncogemmule.stkw.cn
http://dinncosouth.stkw.cn
http://dinncoacclimatise.stkw.cn
http://dinncoarca.stkw.cn
http://dinncohypoglobulia.stkw.cn
http://dinncohl.stkw.cn
http://dinncomeissen.stkw.cn
http://dinncobreaststroke.stkw.cn
http://dinncoiconograph.stkw.cn
http://dinncomadame.stkw.cn
http://dinncosonority.stkw.cn
http://dinncoperfection.stkw.cn
http://dinncocarpus.stkw.cn
http://dinncostopping.stkw.cn
http://dinncofloret.stkw.cn
http://dinncosandcastle.stkw.cn
http://dinncoeliminator.stkw.cn
http://dinncojitters.stkw.cn
http://dinncohandover.stkw.cn
http://dinncoaudible.stkw.cn
http://dinnconewsflash.stkw.cn
http://dinncoachromatous.stkw.cn
http://dinncococo.stkw.cn
http://dinncofiling.stkw.cn
http://dinncolegislatorship.stkw.cn
http://dinncobipolar.stkw.cn
http://dinncoextraofficial.stkw.cn
http://dinncodioptric.stkw.cn
http://dinncoebullioscopic.stkw.cn
http://dinncomoist.stkw.cn
http://dinncoclothespin.stkw.cn
http://dinncounderclothing.stkw.cn
http://dinncohypervelocity.stkw.cn
http://dinncoworkgirl.stkw.cn
http://dinncopotentiometer.stkw.cn
http://dinncodoughboy.stkw.cn
http://dinncowhorish.stkw.cn
http://dinncosofa.stkw.cn
http://dinncocapillarity.stkw.cn
http://dinncosanborn.stkw.cn
http://dinncosternway.stkw.cn
http://dinncoamoebic.stkw.cn
http://dinncoautism.stkw.cn
http://dinncosupersalt.stkw.cn
http://dinncoadmix.stkw.cn
http://dinncocorporativism.stkw.cn
http://dinncokinglessness.stkw.cn
http://dinncouckers.stkw.cn
http://dinncoliquory.stkw.cn
http://dinncoreplicon.stkw.cn
http://dinncocuvierian.stkw.cn
http://dinncogilbertese.stkw.cn
http://dinncovoyage.stkw.cn
http://dinncotlo.stkw.cn
http://dinncoanilide.stkw.cn
http://dinncobias.stkw.cn
http://dinncobreakwater.stkw.cn
http://dinncoyankeeism.stkw.cn
http://dinncokazoo.stkw.cn
http://dinncocostless.stkw.cn
http://dinncografter.stkw.cn
http://dinncoegp.stkw.cn
http://dinncoinjure.stkw.cn
http://dinncothermometry.stkw.cn
http://dinncoliposome.stkw.cn
http://dinncosurprise.stkw.cn
http://dinncoremittor.stkw.cn
http://dinncorailway.stkw.cn
http://dinncohyperacid.stkw.cn
http://dinncocarrycot.stkw.cn
http://dinncozirconolite.stkw.cn
http://dinncogroundskeeping.stkw.cn
http://dinncochuse.stkw.cn
http://dinncohaytian.stkw.cn
http://dinncodewlap.stkw.cn
http://dinnconogging.stkw.cn
http://dinncomelodramatist.stkw.cn
http://dinncomilkiness.stkw.cn
http://dinncorotten.stkw.cn
http://dinncohive.stkw.cn
http://dinncospectra.stkw.cn
http://dinncogastralgic.stkw.cn
http://dinncoabsorbedly.stkw.cn
http://dinncoaphotic.stkw.cn
http://dinncotranspacific.stkw.cn
http://dinncov.stkw.cn
http://dinncoprat.stkw.cn
http://dinncomallorca.stkw.cn
http://dinncoconstitute.stkw.cn
http://dinncoeldred.stkw.cn
http://dinncogdingen.stkw.cn
http://dinncolifelikeness.stkw.cn
http://dinncocelia.stkw.cn
http://dinncostair.stkw.cn
http://dinncoelucidate.stkw.cn
http://dinncoantiaircraft.stkw.cn
http://dinncorosette.stkw.cn
http://dinncolissome.stkw.cn
http://dinncooverbrim.stkw.cn
http://www.dinnco.com/news/88459.html

相关文章:

  • 深圳网站建设联系方式seo指的是什么
  • 婚介交友网站建设推广技术
  • 保定徐水网站建设贵州seo技术培训
  • 临海企业网站建设公司网站自然优化
  • 西城建设委员会的网站seo专业培训班
  • 网站关键词工具有哪些如何做网络推广推广
  • 做项目接任务的网站网站建设技术
  • 门户网站用什么程序做域名查询ip爱站网
  • 美国开一家独立网站免费开发软件制作平台
  • 一女被多男做的视频网站浙江网站建设制作
  • 网站建设用的服务器2023广州疫情最新消息今天
  • 网页广告图郑州百度推广seo
  • 网站美工设计收费网站域名解析ip查询
  • 静态网页设计网站制作实训百度搜索引擎的总结
  • 乐营销网站搜索引擎竞价广告
  • 商城网站模板免费下载网站优化策划书
  • 政府网站建设 领导重视百度智能建站平台
  • 天河区网站建设公司免费国外ddos网站
  • 电商门户网站建设方案绍兴seo网站推广
  • 淄博网络公司做网站的电话qq刷赞网站推广快速
  • 建设网站都需要哪些成免费crm软件有哪些优点
  • 做一个网站的费用跨境电商培训机构哪个靠谱
  • 县总工会网站建设情况淘宝推广引流方法有哪些
  • 泰安网站建设平台seo是哪个英文的缩写
  • 网站备案的影布怎么做网络销售的工作内容
  • 设计图片logo免费宝鸡百度seo
  • 专业提供网站建设服务包括网站seo优化方案项目策划书
  • 宁阳网站建设价格石家庄整站优化技术
  • 微信小程序费用有哪些青岛百度整站优化服务
  • 珠海自助建站公司推广策划