We know that if you send a large file in the way below:
ws.send(file);
you may probably end up receiving an error saying it is too big and the connection is closed immediately
How will you design your application to send a large file, in FULL speed?
My solution:
async async_timeout(t) {
return new Promise((resolve, reject) => {
setTimeout(() => { resolve(); }, t);
});
}
async wait_websocket_send(ws) {
// 262144 must be less that the max value of WebSocket.bufferedAmount
while (ws.bufferedAmount > 262144)
await async_timeout(2);
}
async send_large_data(ws, data) {
let reader = data.stream.getReader();
while (true) {
let chunk = await reader.read();
if (chunk.done)
break;
// wait for the queued data to go
await wait_websocket_send(ws);
ws.send(chunk.value);
}
}
I tested it, but it seems to transmit in a lower speed than my full bandwidth
How can I improve the performance?