我正在用WebRTC技术做一些测试,但是Chrome和火狐之间的RTCDataChannel有一些问题。连接工作正常,我可以在两个浏览器之间发送数据,但是当两个浏览器中的一个断开时,问题就出现了。
当Chrome发送数据,Firefox接收数据和Firefox断开连接时,Chrome侧的通道设置它的readyState关闭,我可以处理显示消息。
当火狐发送数据而Chrome接收数据和Chrome断开连接时,火狐端中的通道不会改变它的readyState,所以我无法处理这个问题,而且很明显火狐继续将数据发送到关闭的通道?
我的代码很简单:
建立联系:
[...]
if(offerer){
channel = connection.createDataChannel("dataChannel", null);
setChannelEvents(channel);
connection.createOffer(myFunc, showError);
}else{
connection.ondatachannel = function(event){
channel = event.channel;
setChannelEvents(channel);
}
}
[...]当建立连接并可以发送消息时:
[...]
try{
channel.send(message);
}catch(e){
//handle error here
[...]
}
[...]正如我说过的,当在Chrome channel.send( message)中返回错误(因为readyState被设置为关闭并不能发送消息)时,'handle‘中的代码会执行,但在火狐中不会执行。
有人能帮帮我吗?有点让人沮丧。谢谢!
发布于 2016-05-26 03:36:42
试试我的代码:
switch (channel.readyState) {
case 'open':
channel.send(message);
break;
case 'closed':
console.log('channel.readyState = ' + channel.readyState);
//handle error here
break;
default:
console.error('channel.readyState = ' + channel.readyState);}
我已经成功地在Chrome和Firefox上测试了
发布于 2016-06-03 01:28:55
调用接收端的RTCDataChannel.close()方法。
https://stackoverflow.com/questions/37318166
复制相似问题