最近我一直在用WebRTC做实验。我试着把我的头绕在它周围,同时看看我是否能测试它。
所以我写了一个测试来检查两件事
我在考试中注意到的是,(1)不是真的。我所发现的是,这个测试的平均结果是100次中的10到20次?
我要求的是有人看一看,让我知道我是否写错了我的测试,或者也许找出了失败的另一个原因。
请注意,我只在Chrome 62上运行这个程序,冰服务器列表是从https://github.com/DamonOehlman/freeice获取的。
下面是我写的一个测试:
const localStream = await navigator.mediaDevices.getUserMedia({video:true});
const iceServers = [{
urls: [
"stun:stun.l.google.com:19302",
"stun:stun1.l.google.com:19302",
"stun:stun2.l.google.com:19302",
"stun:stun3.l.google.com:19302",
"stun:stun4.l.google.com:19302",
"stun:stun.ekiga.net",
"stun:stun.ideasip.com",
"stun:stun.rixtelecom.se",
"stun:stun.schlund.de",
"stun:stun.stunprotocol.org:3478",
"stun:stun.voiparound.com",
"stun:stun.voipbuster.com",
"stun:stun.voipstunt.com",
"stun:stun.voxgratia.org"
]
}]
async function initExample(){
try{
// Init Local Connections
var localConnection = new RTCPeerConnection({iceServers});
var remoteConnection = new RTCPeerConnection({iceServers});
// Create Data Channels
var remoteDataChannel;// this is added later
var localDataChannel = localConnection.createDataChannel('DataChannel');
// Configure Stream
var remoteStream;// this is added later
localConnection.addStream(localStream)
// Catch DataChannel when it is sent from local to remote
remoteConnection.ondatachannel = dataChannelEvent => {
remoteDataChannel = dataChannelEvent.channel
}
// Catch MediaStream when it is sent from local to remote
remoteConnection.onaddstream = mediaStreamEvent => {
remoteStream = mediaStreamEvent.stream;
}
// Create Offer
var offer = await localConnection.createOffer();
await localConnection.setLocalDescription(offer);
await remoteConnection.setRemoteDescription(localConnection.localDescription);
// Create Answer
var answer = await remoteConnection.createAnswer();
await remoteConnection.setLocalDescription(answer);
await localConnection.setRemoteDescription(remoteConnection.localDescription);
// Return Results
return {
localConnection, remoteConnection,
localDataChannel, localStream,
get remoteDataChannel(){
return remoteDataChannel
},
get remoteStream(){
return remoteStream;
},
}
}catch(e){
console.error(e)
}
}
async function test(N=100){
const stats = {pass:0, fail: 0, N};
const results = [];
for (var i = 0; i < N; i++) {
console.log({...stats, i})
let conn = await initExample();
if(conn.remoteDataChannel){
stats.pass++;
}else{
stats.fail++;
}
results.push(conn);
}
return results;
}
var r = await test();发布于 2017-12-05 18:11:45
因此,在@JTejedor的评论和建议之后,我意识到我的连接测试设置错误。对于任何有此问题的人,请注意,您需要确保同侪正在发送他们的注册冰候选人到对方。
在我的脱机版本中,这样做如下:
// Map Ice Candidate
remoteConnection.onicecandidate = evt => {
if(evt.candidate) {
localConnection.addIceCandidate(evt.candidate)
}
}
localConnection.onicecandidate = evt => {
if(evt.candidate) {
remoteConnection.addIceCandidate(evt.candidate)
}
}如果使用套接字(或其他服务器端解决方案),请确保通过套接字将这些套接字发送给其他对等方。
下面是工作测试(总是通过):
// Ice Servers
const iceServers = [{
urls: [
"stun:stun.l.google.com:19302",
"stun:stun1.l.google.com:19302",
"stun:stun2.l.google.com:19302",
"stun:stun3.l.google.com:19302",
"stun:stun4.l.google.com:19302",
"stun:stun.ekiga.net",
"stun:stun.ideasip.com",
"stun:stun.rixtelecom.se",
"stun:stun.schlund.de",
"stun:stun.stunprotocol.org:3478",
"stun:stun.voiparound.com",
"stun:stun.voipbuster.com",
"stun:stun.voipstunt.com",
"stun:stun.voxgratia.org"
]
}]
// Run the test once
async function initExample(){
// Init Local Connections
var localConnection = new RTCPeerConnection({iceServers});
var remoteConnection = new RTCPeerConnection({iceServers});//this is really "remote" its simulated
// Map Ice Candidate
remoteConnection.onicecandidate = evt => {
if(evt.candidate) {
localConnection.addIceCandidate(evt.candidate)
}
}
localConnection.onicecandidate = evt => {
if(evt.candidate) {
remoteConnection.addIceCandidate(evt.candidate)
}
}
// Create Data Channels
var remoteDataChannel;// this is added later
var localDataChannel = localConnection.createDataChannel('DataChannel');
// Tie end of promise to getting the datachannel
return new Promise( async res => {
// Catch DataChannel when it is sent from local to remote
remoteConnection.ondatachannel = dataChannelEvent => {
console.log('GOT CHANNEL', dataChannelEvent)
remoteDataChannel = dataChannelEvent.channel
res(remoteDataChannel);
}
// Create Offer
var offer = await localConnection.createOffer();
await localConnection.setLocalDescription(offer);
await remoteConnection.setRemoteDescription(localConnection.localDescription);
// Create Answer
var answer = await remoteConnection.createAnswer();
await remoteConnection.setLocalDescription(answer);
await localConnection.setRemoteDescription(remoteConnection.localDescription);
})
}
// A Promisified Timeout
const promisedTimeout = (timeout = 10000) => new Promise((resolve, reject) => {
let wait = window.setTimeout(() => {
window.clearTimeout(wait);
resolve(false);
}, timeout)
})
// Run the test N times
async function test(N=100){
// Init variables
const stats = {pass:0, fail: 0, N};
const results = [];
// Loop
for (var i = 0; i <= N; i++) {
// log
console.log({...stats, i});
// This will each example against a timeout,
// if the timeout finishes first, then this test will be marked as fail
let pass = await Promise.race([
initExample(),
promisedTimeout()
]);
if(pass) {
stats.pass ++;
}else{
stats.fail ++;
}
results.push(pass);
}
return {results,stats};
}
// Run the test
test()https://stackoverflow.com/questions/47657337
复制相似问题