我尝试使用WebRTC's adapter.js通过带有RTCDataChannel的RTCPeerConnection发送文本,但收到以下错误:
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");我做错什么了吗?
发布于 2014-03-18 22:13:29
今天早上,我编写了以下代码,在单个页面中使用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);
});
});发布于 2014-03-18 16:22:54
你不能只创建peerConnection、dataChannel,然后马上开始使用它。顺便说一句,你在这里没有两个同龄人。
>G29
我建议从阅读this开始,它会让你了解基本概念,然后继续山姆·达顿的this And代码实验室。
更新以响应mhenry的请求:以下是在一个类中设置数据通道的全部内容:https://gist.github.com/shacharz/9661930遵循注释,您只需:
Handlmessage添加信令,将sdp的ice候选发送到您希望通过更高级别的逻辑处理所有连接中断之类的其他对等点。
https://stackoverflow.com/questions/22470291
复制相似问题