我有两个客户:
1)Windows 7 PC上的Chrome(版本50.0.2661.102 m)
2)Android平板电脑上的Chrome(版本50.0.2661.89)
两者都在同一个网络中(因此不需要STUN / TURN服务器)。
我在带有Centos 6的VirtualBox VM上使用我自己的node.js(webSocket)构建的信号服务器。
客户端之间的视频/声音通信工作正常。现在我想将文件从一个客户端传输到另一个客户端。作为我的代码的基础,我使用此示例的代码 here
正如这段代码所示,我在创建PeerConnection之后创建了dataChannnel。
function createPeerConnection() {
....
myPeerConnection = new RTCPeerConnection(iceServers, optional);
myDataChannel = myPeerConnection.createDataChannel('myDataChannel');
// Set up event handlers for the ICE negotiation process.
myPeerConnection.onicecandidate = handleICECandidateEvent;
myPeerConnection.onaddstream = handleAddStreamEvent;
myPeerConnection.onnremovestream = handleRemoveStreamEvent;
myPeerConnection.oniceconnectionstatechange = handleICEConnectionStateChangeEvent;
myPeerConnection.onicegatheringstatechange = handleICEGatheringStateChangeEvent;
myPeerConnection.onsignalingstatechange = handleSignalingStateChangeEvent;
myPeerConnection.onnegotiationneeded = handleNegotiationNeededEvent;
myPeerConnection.ondatachannel = handleDataChannel;
myDataChannel.onmessage = handleDataChannelMessage;
myDataChannel.onopen = handleDataChannelOpen;
}
...
...
function invite(peerId) {
...
createPeerConnection();
...
}
...
...
function handleVideoOfferMsg(msg) {
thereIsNegotiation = true;
targetUsername = msg.name;
// Call createPeerConnection() to create the RTCPeerConnection.
log("Starting to accept invitation from " + targetUsername);
createPeerConnection();
// We need to set the remote description to the received SDP offer
// so that our local WebRTC layer knows how to talk to the caller.
var desc = new RTCSessionDescription(msg.sdp);
myPeerConnection.setRemoteDescription(desc)
.then(function(stream) {
log("-- Calling myPeerConnection.addStream()");
return myPeerConnection.addStream(localStream);
})
.then(function() {
log("------> Creating answer");
// Now that we've successfully set the remote description, we need to
// start our stream up locally then create an SDP answer. This SDP
// data describes the local end of our call, including the codec
// information, options agreed upon, and so forth.
return myPeerConnection.createAnswer();
})
.then(function(answer) {
log("------> Setting local description after creating answer");
// We now have our answer, so establish that as the local description.
// This actually configures our end of the call to match the settings
// specified in the SDP.
return myPeerConnection.setLocalDescription(answer);
})
.then(function() {
var msg = {
name: clientId,
room: roomId,
target: targetUsername,
type: "video-answer",
sdp: myPeerConnection.localDescription
};
// We've configured our end of the call now. Time to send our
// answer back to the caller so they know that we want to talk
// and how to talk to us.
log("Sending answer packet back to other peer");
sendToServer(msg);
})
.catch(handleGetUserMediaError);
}
当第二个客户提出要约时,第一个客户端在尝试回答时,我收到错误
打开相机和/或麦克风时出错:无法设置本地应答 spd:无法按下传输描述:提供本地指纹 但没有身份证明。
或
打开相机和/或麦克风时出错:无法设置本地应答 spd:调用错误状态:STATE_INPROGRESS
只有一次创作成功。
我是否必须在其他地方创建DataChannel?像这里:
function handleICEConnectionStateChangeEvent {
switch(myPeerConnection.iceConnectionState) {
...
case "connected":
createDataChannel();
break;
}
}
function createDataChannel(){
myDataChannel = myPeerConnection.createDataChannel('myDataChannel');
myPeerConnection.ondatachannel = handleDataChannel;
myDataChannel.onmessage = handleDataChannelMessage;
myDataChannel.onopen = handleDataChannelOpen;
}
有什么建议吗?
答案 0 :(得分:2)
此代码中的错误是发件人和收件人都创建了新的数据通道。正确的是,创建数据通道
myDataChannel = myPeerConnection.createDataChannel('myDataChannel')
和另一个等待创建dataChannel:
myPeerConnection.ondatachannel = handleDataChannel;