已经建立对等连接,有没有一种方法可以创建数据通道?
下面是我的操作步骤:
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
状态。