C
C
Chainsaw21002021-12-19 20:16:11
JavaScript
Chainsaw2100, 2021-12-19 20:16:11

WebRTC / Promise - Why is the second client getting zeros and not bytes?

Hello, I am making a WebRTC-based video chat with support for sending files.
There are asynchronous functions, promises, etc.
Now I send from one client to another file, and if it is more than def. numbers, it is divided into chunks (everything is logical). First, a blob is taken, an arrayBuffer is made from it, and it is sent. It seems to ship just fine. I looked at the received bytes on the second client, it seems to be ok, but when I try to compose the arrayBuffer into a blob for the second side, only zeros are obtained. I sin on asynchrony. Can you suggest? Thank you.

async function convertToArrayBuffer(file) {
const buf = await file.arrayBuffer();
return buf;
};

const shareFile = () => {
if (file) {
const channelLabel = file.name;
for (const [key, value] of Object.entries(mapPeers)) {
const channel = value[1];
channel.binaryType = 'arraybuffer';
let inf = convertToArrayBuffer(file);
inf.then((buf) => {
for (let i = 0; i < buf.byteLength; i += MAXIMUM_MESSAGE_SIZE) {
channel.send(buf.slice(i, i + MAXIMUM_MESSAGE_SIZE));
}
channel.send(END_OF_FILE_MESSAGE);
});
channel.onclose = () => {
closeDialog();
};
}
}
else{
console.log('no file');
}
};

function dcOnMessage(peer, event){
var message = event.data;
if (typeof message === "string" && message != 'EOF'){
var li = document.createElement('li');
li.appendChild(document.createTextNode(message));
messageList.appendChild(li);
} else {
const { data } = event;
try {
if (data !== END_OF_FILE_MESSAGE) {
receivedBuffers.push(data);
} else {
const arrayBuffer = receivedBuffers.reduce((acc, arrayBuffer) => {
const tmp = new Uint8Array(acc.byteLength + arrayBuffer.size);
tmp.set(new Uint8Array(acc), 0);
let inf2 = test(arrayBuffer);
inf2.then((buf) => {
tmp.set(new Uint8Array(buf), acc.byteLength);
});
return tmp;
}, new Uint8Array());
const blob = new Blob([arrayBuffer]);
downloadFile(blob, event.explicitOriginalTarget.label);
receivedBuffers = []
}
} catch (err) {
console.log(err, 'File transfer failed');
}
};
};

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question