gpt4 book ai didi

javascript - RTCDataChannel 的 ReadyState 不是 'open'

转载 作者:数据小太阳 更新时间:2023-10-29 04:40:37 27 4
gpt4 key购买 nike

我正在尝试使用 WebRTC's adapter.js 通过 RTCPeerConnectionRTCDataChannel 发送文本,但出现以下错误:

Uncaught InvalidStateError:
Failed to execute 'send' on 'RTCDataChannel':
RTCDataChannel.readyState is not 'open'

我的代码可以通过 this fiddle 获得及以下:

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");

我做错了什么吗?

最佳答案

我今天早上写了下面的代码,在一个页面中使用了 RTCPeerConnectionRTCDataChannel。声明这些函数的顺序很重要。

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);
});
});

关于javascript - RTCDataChannel 的 ReadyState 不是 'open',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22470291/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com