我正在开发一个使用webrtc为两个人创建会议的APP。 实际上,我正在使用socket.io进行信号传输,并同时使用服务器眩晕和服务器转向。 (我是从http://xirsys.com/购买这些服务器的。)
最后,我在整个webrtc中都进行了连接,但是几分钟后,图像或视频被冻结,有时我错过了连接。
我对程序有误吗?如果发生任何情况,如何监视连接以重新连接?
完整的代码在Github中:https://github.com/lfdeiby/playwebrtc.git
并在Heroku中测试网站:https://gentle-reef-80146.herokuapp.com/
io.on('signaling_message', async ({data: {description, candidate}}) => {
displaySignalMessage("Signal received");
try{
if (description){
const offerCollision = (description.type == 'offer') && (makingOffer || pc.signalingState != 'stable');
ignoreOffer = !polite && offerCollision;
if( ignoreOffer )
return;
if (offerCollision) {
await Promise.all([
pc.setLocalDescription({type: "rollback"}),
pc.setRemoteDescription(description)
]);
} else {
await pc.setRemoteDescription(description);
}
if( description.type == "offer" ){
const answer = await pc.createAnswer();
await pc.setLocalDescription(answer);
io.emit('signal', {description: pc.localDescription, room: SIGNAL_ROOM});
}
}else if(candidate){
try{
await pc.addIceCandidate(candidate);
}catch(err){
displaySignalMessage(err.message);
if(!ignoreOffer){
throw err;
}
}
}
}catch(err){
console.log(err.message);
INFO.error_signaling(err.message);
}
});
function startSignaling() {
displaySignalMessage("starting signaling...");
pc = new RTCPeerConnection(config);
for (const track of localStream.getTracks()) {
pc.addTrack(track, localStream);
}
if( polite == false ){
dataChannel = pc.createDataChannel('chat', null);
dataChannel.onopen = function(){
if( dataChannel.readyState == 'open' ){
displaySignalMessage("Data Channel is ready");
dataChannel.onmessage = dataChannelMessage;
dataChannel.onclose = function(){ displaySignalMessage("Remote close conection"); closePeerConnection(); }
}
}
}else {
pc.ondatachannel = function(event){
dataChannel = event.channel;
dataChannel.onmessage = dataChannelMessage;
dataChannel.onclose = function(){ displaySignalMessage("Remote close conection"); closePeerConnection(); }
}
}
pc.onicecandidate = function(event){
if (event.candidate){
displaySignalMessage("Signal send: " + event.candidate);
io.emit('signal', {candidate: event.candidate, room: SIGNAL_ROOM});
}
};
pc.oniceconnectionstatechange = function(event){
console.log("ICE CHANGE ", pc.iceConnectionState, event);
}
pc.onnegotiationneeded = function(){
if( polite == false ){
makingOffer = true;
pc.createOffer(sdpConstraints)
.then(function(offer){
if (pc.signalingState != "stable") return;
return pc.setLocalDescription(offer);
})
.then(function(){
io.emit('signal', {description: pc.localDescription, room: SIGNAL_ROOM});
})
.catch(function(err){
displaySignalMessage("ERROR OFFER", err.message);
});
}
}
pc.oniceconnectionstatechange = function(evt){
displaySignalMessage("ICE connection state change: " + evt.target.iceConnectionState);
if (pc != null && pc.iceConnectionState === "failed") {
pc.restartIce();
}
};
pc.onaddstream = function (event) {
videoRemote.srcObject = event.stream;
signalingFinalizeSuccess();
};
pc.ontrack = ({track, streams}) => {
track.onunmute = () => {
if (videoRemote.srcObject) return;
videoRemote.srcObject = streams[0];
};
signalingFinalizeSuccess();
};
pc.onerror = function(err){
displaySignalMessage("PC ERROR: " + err.message);
console.log(err);
}
function dataChannelMessage(data){
console.log("datachannelReceive", data);
displaySignalMessage(data);
}
}