我正在使用浏览器连接到的中央电子应用程序构建WebRTC应用程序。我正在测试的方案是在计算机(Ubuntu 16.04)上运行电子应用程序,并从Android(7.0)上的Chrome(69)连接。在调试之后,要约,答案和候选对象都已通过,但在生成连接的最后一站失败。冰连接状态切换为“正在检查”然后“失败”。我可以将浏览器应用程序加载到笔记本电脑上,并连接到同一台计算机上的电子应用程序。
我应该使用一组冰服务器吗?我需要什么来使WebRTC连接更健壮?我不应该编写自己的信令过程并使用预制的东西吗?他们是否仍然要调试连接失败的原因?对于调试,我尝试了chrome上的webrtc调试选项卡,但它说的只是连接失败在堆栈底部。
我的RTCPeerConnection的配置是:
const configuration = {
"iceServers": [{ "urls": "stun:stun.1.google.com:19302" }]
};
这是我用来形成从浏览器应用程序到电子应用程序的连接的代码(该函数附加到ts类,依次调用:setupPeerConnection,setupDataChannel,makeOffer):
setupPeerConnection(gameID:string) {
this.gameID = gameID;
let configuration:RTCConfiguration = {
"iceServers": [{ "urls": "stun:stun.1.google.com:19302" }]
};
this.connection = new RTCPeerConnection(configuration);
const that:PeerConnection = this;
//Definition of the data channel
this.connection.ondatachannel = function(ev:RTCDataChannelEvent) {
};
//When we get our own ICE Candidate, we provide it to the other Peer.
this.connection.onicecandidate = function(event:RTCPeerConnectionIceEvent) {
if (event.candidate && that.connectedToGame) {
that.serverConnection.send({
type: "candidate",
candidate: event.candidate
});
}
};
this.connection.oniceconnectionstatechange = function(e:Event) {
let iceState:RTCIceConnectionState = this.iceConnectionState;
console.log("Changing connection state:", iceState)
if (iceState == "connected") {
console.log("Connection established with server");
} else if (iceState =="disconnected" || iceState == "closed") {
// We lost the user
that.connectedToGame = false;
that.onLeave();
}
};
}
setupDataChannel(onopen:(error:ErrorEvent)=>void, onmessage:(message:MessageEvent)=>void) {
let dataChannelOptions:RTCDataChannelInit = <RTCDataChannelInit>{
reliable: true
};
this.dataChannel = this.connection.createDataChannel(this.gameID + "-dataChannel", dataChannelOptions);
this.dataChannel.onerror = function (error:ErrorEvent) {
console.log("Error on data channel:", error);
};
this.dataChannel.onmessage = onmessage.bind(this);
this.dataChannel.onopen = onopen;
this.dataChannel.onclose = function() {
console.log("Channel closed.");
};
}
makeOffer() {
let that:PeerConnection = this;
this.connection.createOffer().then(function (offer:RTCSessionDescriptionInit) {
that.serverConnection.send({
gameID: that.gameID,
type: "offer",
offer: offer
});
that.connection.setLocalDescription(offer);
}, function (error) {
console.log("Error contacting remote peer: ", error);
});
}