我一直在尝试使用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;
在此后的创建频道中:
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"
我开始认为它是chrome的版本..我无法让GitHub示例在chrome中工作:(步骤4进行数据聊天) https://bitbucket.org/webrtc/codelab
虽然相同的代码在Mozilla中有效。
来自提供者的sendChannel有一个" readyState" "连接"
任何帮助非常感谢。