我一直在尝试使用webrtc进行数据聊天 . 它以前在谷歌浏览器中工作并且突然停止工作,我已经将问题缩小到'ondatachannel'回调函数没有被触发 . 完全相同的代码在Mozilla中运行良好 .
这是整体代码:
app.pc_config =
{'iceServers': [
{'url': 'stun:stun.l.google.com:19302'}
]};
app.pc_constraints = {
'optional': [
/* {'DtlsSrtpKeyAgreement': true},*/
{'RtpDataChannels': true}
]};
var localConnection = null, remoteConnection = null;
try {
localConnection = new app.RTCPeerConnection(app.pc_config, app.pc_constraints);
localConnection.onicecandidate = app.setIceCandidate;
localConnection.onaddstream = app.handleRemoteStreamAdded;
localConnection.onremovestream = app.handleRemoteStreamRemoved;
}
catch (e) {
console.log('Failed to create PeerConnection, exception: ' + e.message);
return;
}
isStarted = true;
在创建 Channels 之后:
var localConnection = app.localConnection;
var sendChannel = null;
try {
sendChannel = localConnection.createDataChannel(app.currentchannel,
{reliable: false});
sendChannel.onopen = app.handleOpenState;
sendChannel.onclose = app.handleCloseState;
sendChannel.onerror = app.handleErrorState;
sendChannel.onmessage = app.handleMessage;
console.log('created send channel');
} catch (e) {
console.log('channel creation failed ' + e.message);
}
if (!app.isInitiator){
localConnection.ondatachannel = app.gotReceiveChannel;
}
app.sendChannel = sendChannel;
我创建优惠:
app.localConnection.createOffer(app.gotLocalDescription, app.handleError);
和答案:
app.localConnection.createAnswer(app.gotLocalDescription, app.handleError);
成功创建提供和答案,交换候选人并在两端触发onicecandidate事件!本端描述和RemoteDescription在两端设置 .
我有一个用于信令的推送服务器,我能够通过推送服务器成功发送和接收消息 .
相同的webrtc代码适用于audio / video = true,唯一的问题是当我尝试创建datachannel时 . 唯一没有执行的步骤是没有执行回调函数,即“gotReceiveChannel”
我'm starting to think it'是chrome的版本..我无法在Chrome中使用GitHub示例:(步骤4进行数据聊天)https://bitbucket.org/webrtc/codelab
虽然相同的代码在Mozilla中有效 .
来自提供者的sendChannel具有“连接”的“readyState”
任何帮助非常感谢 .