WebRTC dataChannel.send这个发送的数据有长度限制吗

misterpan
  • 843
// Create the local connection and its event listeners

localConnection = new RTCPeerConnection();

// Create the data channel and establish its event listeners
sendChannel = localConnection.createDataChannel("sendChannel");
sendChannel.onopen = handleSendChannelStatusChange;
sendChannel.onclose = handleSendChannelStatusChange;
localConnection.ondatachannel = receiveChannelCallback;
sendChannel.onmessage = handleReceiveMessage;

//函数明细省略

sendChannel.send('aaaa'); // 发送成功
sendChannel.send(new ArrayBuffer(111)); // 成功
sendChannel.send(new ArrayBuffer(872494)); // 发送之后sendChannel通道自动关闭了,数据也没发送成功,sendChannel.bufferedAmount // 872494


// 发送文件

var read = new FileReader()
                    read.readAsArrayBuffer(file) // file = input file
                    read.onloadend = () => {
                        sendChannel.send(read.result)
                        console.log(sendChannel) // 状态为closed
                        console.log(read.result) // arraybuffer
                    }

不知道是不是有什么限制,在发送input中输入的文本没有问题,当我想发送image文件时就挂掉了.还请大佬能指点一下。

回复
阅读 3.8k
2 个回答

webrtcDataChannel 是有缓冲区的,超过缓冲区大小就会关闭通道,好像是15M?具体错误原因还是看 sendChannel.onerror 的报错吧

通道的传送的数据大小确实有限制。经测试,在chrome浏览器中,最大发送字节为262144,即256kb

sendChannel.send(new ArrayBuffer(262144)); // success
sendChannel.send(new ArrayBuffer(262145)); // failed
宣传栏