Reputation: 11
I am using libdatachannel library and i want to send file content to answerer.
I have dataChannel but when i sending file content via datachannel, i got error "Message size exceeds limit". i don't know how to split into chunks my message.(setting maxMessageSize is not option because file size changes according to offerer and answerer is browser)
I tried simply;
void sendfile2Content(std::shared_ptr<rtc::DataChannel> dc, const std::map<std::wstring, std::wstring>& files)
{
nlohmann::json msg
{
{"Message_Type", "File"},
{"FileName_to_Contents", files}
};
dc->send(msg.dump());
}
When i call the function i got std::logic_error(Message size exceeds limit).
How to send message to peer when message size over the message size limit?
Upvotes: 1
Views: 824
Reputation: 684
Even if data channels support arbitrarily large messages provided the maximum size is negotiated beforehand when establishing the peer connection, you should not send large files as single messages. Instead, you should split them into multiple chunks smaller than the max message size (for instance 64KB), send them as messages, and reassemble each file at destination.
For instance, you can open a data channel per file (reliable and ordered), send the chunked file as binary messages (possibly with a first text message containing metadata), and close it when finished. The remote side reconstructs the file by appending messages until the channel is closed.
The best practice to prevent gobbling memory is to read the file gradually, stop sending when bufferedAmount
is over the threshold, and wait for the onBufferedAmountLow
callback to resume sending.
Upvotes: 1
Reputation: 5651
SCTP (the protocol used by WebRTC datachannels) limits the size of messages to 16kB, and while there exist protocol extensions to send larger messages, they have serious problems, such as not being interoperable between implementations and causing head-of-line blocking. Thus, you should structure your code to limit messages to 16kB.
In order to implement file transfer over datachannels, you need to manually split your data into 16kB chunks at the sender, and reassemble the chunks at the receiver. Since the datachannel API predates promises, you will also need to wait for the onbufferedamountlow
callback in order to avoid overwhelming the socket buffer.
There's an example in the WebRTC samples, and there's an implementation using promises in the Galene sources (disclaimer, I am the author).
Upvotes: 0