HarmonyOS PB转ArrayBuffer发送失败

使用原本PB转换后的Uint8Array,取Buffer方式发送,提示:

send fail:{“code”:2301104,“message”:“Connection reset by peer”}

on error, err:{“code”:107}

uint8ArrayToString(fileData : Uint8Array){
  let dataString = "";
  for (let i = 0; i < fileData.length; i++) {
    dataString += String.fromCharCode(fileData[i]);
  }
  return dataString
}

// 然后可以通过下面的函数把string转换为ArrayBuffer
strToArrayBuffer(str: string) {
  let buf = new ArrayBuffer(str.length * 2);
  let bufView = new Uint16Array(buf);
  for (let i = 0, strLen = str.length; i < strLen; i++) {
    bufView[i] = str.charCodeAt(i);
  }
  return bufView;
}

sendData(data:Uint8Array) {
  let socketMessage = SocketMessage.SocketMessage.create({payload:data,payloadType:SocketMessage.PayloadType.CS_ENTER_ROOM,compressionType:SocketMessage.SocketMessage.CompressionType.NONE});
  // let arrayBuffer = SocketMessage.SocketMessage.encode(socketMessage).finish().buffer;
  let uint8Array : Uint8Array = SocketMessage.SocketMessage.encode(socketMessage).finish();
  let str:string = this.uint8ArrayToString(uint8Array);
  let arrayBuffer = this.strToArrayBuffer(str);
  let tcpSendOptions: socket.TCPSendOptions = {
    data: arrayBuffer
  }
  this.tcp.send(tcpSendOptions, (err: BusinessError) => {
    if (err) {
      LiveLogger.i(TAG,'send fail' + JSON.stringify(err));
      return;
    }
    LiveLogger.i(TAG,'send success');
  })
}
HarmonyOS
2024-12-27 16:38:06
浏览
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
fox280
strToArrayBuffer(str: string) {
  let buf = new ArrayBuffer(str.length * 2);
  let bufView = new Uint16Array(buf);
  for (let i = 0, strLen = str.length; i < strLen; i++) {
    bufView[i] = str.charCodeAt(i);
  }
  return buf;
}


let buffer = value.message;
let dataView = new DataView(buffer);
let str = "";
for (let i = 0; i < dataView.byteLength; ++i) {
  str += String.fromCharCode(dataView.getUint8(i));
}
分享
微博
QQ
微信
回复
2024-12-27 19:42:57
相关问题
HarmonyOS strArrayBuffer
247浏览 • 1回复 待解决
HarmonyOS arraybufferbase64乱码
309浏览 • 1回复 待解决
ArrayBuffer怎么Uint8Array?
705浏览 • 1回复 待解决
ArrayBuffer怎样字符串
693浏览 • 1回复 待解决
HarmonyOS Uint8Array怎么正确ArrayBuffer
1673浏览 • 1回复 待解决
HarmonyOS JSON对象失败
392浏览 • 1回复 待解决
HarmonyOS postman 发送推送消息失败
639浏览 • 0回复 待解决
如何将Uint8ArrayArrayBuffer
627浏览 • 1回复 待解决
HarmonyOS 国密SM2换公钥失败
218浏览 • 1回复 待解决
应用在后台时发送请求失败问题
752浏览 • 1回复 待解决
HarmonyOS pixelMap转成ArrayBuffer
348浏览 • 1回复 待解决
HarmonyOS Napi Arraybuffer无法创建
163浏览 • 1回复 待解决
HarmonyOS Array如何转成ArrayBuffer
31浏览 • 1回复 待解决
HarmonyOS 图片pdf
162浏览 • 1回复 待解决
在 string 转成ArrayBuffer报错
1102浏览 • 1回复 待解决