起初,我希望你明白我不擅长英语。
对于文件传输,在网格拓扑中,在Chrome中,
当缓冲量为16MB时,通道将关闭并显示错误消息。
“未捕获的网络错误:无法在'RTCDataChannel'上执行'发送':无法发送数据”
(它是chrome,而不是firefox。)
如何发送文件(超过16MB)?
var chunkSize = 16384;
onmessage = function(event) {
var file = event.data[0],
callbackId = event.data[1],
totalSize = file.size,
chunks = [],
curSize = 0,
reader,
chunk;
if (!FileReaderSync) {
reader = new FileReader();
reader.onload = function(event) {
console.log('chunking...');
chunks.push(event.target.result);
curSize += chunkSize;
slice();
};
slice();
} else {
reader = new FileReaderSync();
while (curSize < totalSize) {
console.log('chunking...');
chunk = file.slice(curSize, curSize + chunkSize);
chunks.push(reader.readAsArrayBuffer(chunk));
curSize += chunkSize;
}
postMessage([chunks, file.name, callbackId]);
}
function slice() {
if (curSize > totalSize) {
postMessage([chunks, file.name, callbackId]);
} else {
chunk = file.slice(curSize, curSize + chunkSize);
reader.readAsArrayBuffer(chunk);
}
}
};
_fileToPeers: function(chunks, fileName, progressCallbackId) {
var callback = callbacks[progressCallbackId],
chunkCount,
peerCount = 0;
objForEach(this.peers, function(peer) {
var channel = peer.fileChannel;
peerCount += 1;
if (channel) {
chunkCount = 0;
chunks.forEach(function(chunk) {
channel.send(chunk);
chunkCount += 1;
console.log('sending...'); // abnormal works!
callback(peerCount, chunkCount); // abnormal works!
});
channel.send(['end', fileName].join(','));
}
});
delete callbacks[progressCallbackId];
},