当对等连接已经建立时,有没有办法创建数据通道?
这是我正在做的事情:
peerConnection.onstatechange = function(event){
var state = peerConnection.readyState;
console.log('state changed to : '+state);
if(state==='stable'){
console.log('connection is stable');
var dataChannel = peerConnection.createDataChannel('test',{reliable: false});
dataChannel.onopen = function(){
console.log('data channel opened');
dataChannel.send('hello data channel');
};
peerConnection.ondatachannel = function(event){
console.log('ondatachannel event fire ',event);
};
}
};
它给了我以下输出:
state changed to : have-local-offer
state changed to : stable
connection is stable
如何确保真正建立了连接?我在这里发现
stable
状态等于active
状态。onopen
事件由于某种原因没有触发,所以我仍然不确定是否建立了连接。
如果您需要更多代码,请告诉我。
编辑:我添加了onnegotiationneeded
事件处理程序,现在peerConnection.ondatachannel
正在触发,但通道处于connecting
状态