如何写精华回答,获更多曝光?
发布
使用原本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');
})
}