我想了一个多发送文本RTCPeerConnection与RTCDataChannel使用的WebRTC的adapter.js,但我发现了以下错误:
Uncaught InvalidStateError:
Failed to execute 'send' on 'RTCDataChannel':
RTCDataChannel.readyState is not 'open'
Run Code Online (Sandbox Code Playgroud)
我的代码可以通过这个小提琴和下面提供:
var peerConnection = new RTCPeerConnection(null, {
optional: [{
RtpDataChannels: true
}]
});
peerConnection.ondatachannel = function(event) {
receiveChannel = event.channel;
receiveChannel.onmessage = function(event){
alert(event.data);
};
};
var dataChannel = peerConnection.createDataChannel("data", {reliable: false});
dataChannel.send("Hello");
Run Code Online (Sandbox Code Playgroud)
我做错了吗?
我今天早上编写了以下代码,使用RTCPeerConnection和RTCDataChannel在一个页面中.声明这些函数的顺序很重要.
var localPeerConnection, remotePeerConnection, sendChannel, receiveChannel;
localPeerConnection = new RTCPeerConnection(null, {
optional: [{
RtpDataChannels: true
}]
});
localPeerConnection.onicecandidate = function(event) {
if (event.candidate) {
remotePeerConnection.addIceCandidate(event.candidate);
}
};
sendChannel = localPeerConnection.createDataChannel("CHANNEL_NAME", {
reliable: false
});
sendChannel.onopen = function(event) {
var readyState = sendChannel.readyState;
if (readyState == "open") {
sendChannel.send("Hello");
}
};
remotePeerConnection = new RTCPeerConnection(null, {
optional: [{
RtpDataChannels: true
}]
});
remotePeerConnection.onicecandidate = function(event) {
if (event.candidate) {
localPeerConnection.addIceCandidate(event.candidate);
}
};
remotePeerConnection.ondatachannel = function(event) {
receiveChannel = event.channel;
receiveChannel.onmessage = function(event) {
alert(event.data);
};
};
localPeerConnection.createOffer(function(desc) {
localPeerConnection.setLocalDescription(desc);
remotePeerConnection.setRemoteDescription(desc);
remotePeerConnection.createAnswer(function(desc) {
remotePeerConnection.setLocalDescription(desc);
localPeerConnection.setRemoteDescription(desc);
});
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6134 次 |
| 最近记录: |