Javascript-函数调用不维护秩序



我有以下代码,其中我调用同一个函数两次,延迟2秒。但是isPermanentDisconnect()内部的所有日志打印都是在控制台中打印log('After secondflag check')之后在checkStatePermanent的末尾打印的。你能告诉我这个代码出了什么问题吗。

function customdelay(miliseconds) {
var currentTime = new Date().getTime();
while (currentTime + miliseconds >= new Date().getTime()) {

}
}
function checkStatePermanent(iceState) {
videoReceivedBytetCount = 0;
audioReceivedByteCount = 0;
log('before First call')
let firstFlag = isPermanentDisconnect();
log('after First call')
log('Disconnect Flag is: ' + firstFlag)
customdelay(2000);
log('after 2 secs')
let secondFlag = isPermanentDisconnect(); //Call this func again after 2 seconds to check whether data is still coming in.
log('after second call')
log('Disconnect Flag is: ' + secondFlag)
if(secondFlag){ //If permanent disconnect then we hangup i.e no audio/video is fllowing
log('Disconnect Flag = Permanent')
if (iceState == 'disconnected'){
hangUpCall(); //Hangup instead of closevideo() because we want to record call end in db
}
}
log('After secondflag check')
...
}

function isPermanentDisconnect(){
var isPermanentDisconnectFlag = false;
var videoIsAlive = false;
var audioIsAlive = false;
myPeerConnection.getStats(null).then(stats => {
stats.forEach(report => {
log('Report Type: '+report.type+ ' Report Kind :'+ report.kind)
if(report.type === 'inbound-rtp' && (report.kind === 'audio' || report.kind  === 'video')){ //check for inbound data only
if(report.kind  === 'audio'){
//Here we must compare previous data count with current
if(report.bytesReceived > audioReceivedByteCount){
// If current count is greater than previous then that means data is flowing to other peer. So this disconnected or failed ICE state is temporary
audioIsAlive = true;
} else {
audioIsAlive = false;

}
log('Previous Audio count: ' +audioReceivedByteCount + ' Now audio count: ' + report.bytesReceived + ' audio is alive: '+ audioIsAlive)
audioReceivedByteCount = report.bytesReceived;
}
if(report.kind  === 'video'){
if(report.bytesReceived > videoReceivedBytetCount){
// If current count is greater than previous then that means data is flowing to other peer. So this disconnected or failed ICE state is temporary
videoIsAlive = true;
} else{
videoIsAlive = false;
}
log('Previous video count: ' +videoReceivedBytetCount + ' Now video count: ' + report.bytesReceived + ' video is alive: ' + videoIsAlive)
videoReceivedBytetCount = report.bytesReceived;
}
if(audioIsAlive || videoIsAlive){ //either audio or video is being recieved.
log('Either video or audio is alive')
isPermanentDisconnectFlag = false; //Disconnected is temp
} else {
isPermanentDisconnectFlag = true;
}
}
})
});
return isPermanentDisconnectFlag;
}

尝试将checkStatePermanent(iceState)设置为异步函数,然后等待对isPermanentDisconnect()的调用。这样,代码执行将暂停,直到isPermanentDisconnect()完成

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function

设法让它工作起来。张贴答案,以防对他人有用。

将延迟功能更改为该

const customdelay = ms => new Promise(res => setTimeout(res, ms));

在以下功能中,请注意asyncawait

async function isPermanentDisconnect (){
var isPermanentDisconnectFlag = false;
var videoIsAlive = false;
var audioIsAlive = false;
await myPeerConnection.getStats(null).then(stats => {
stats.forEach(report => {
if(report.type === 'inbound-rtp' && (report.kind === 'audio' || report.kind  === 'video')){ //check for inbound data only
if(report.kind  === 'audio'){
//Here we must compare previous data count with current
if(report.bytesReceived > audioReceivedByteCount){
// If current count is greater than previous then that means data is flowing to other peer. So this disconnected or failed ICE state is temporary
audioIsAlive = true;
} else {
audioIsAlive = false;

}
log('Previous Audio count: ' +audioReceivedByteCount + ' Now audio count: ' + report.bytesReceived + ' audio is alive: '+ audioIsAlive)
audioReceivedByteCount = report.bytesReceived;
}
if(report.kind  === 'video'){
if(report.bytesReceived > videoReceivedBytetCount){
// If current count is greater than previous then that means data is flowing to other peer. So this disconnected or failed ICE state is temporary
videoIsAlive = true;
} else{
videoIsAlive = false;
}
log('Previous video count: ' +videoReceivedBytetCount + ' Now video count: ' + report.bytesReceived + ' video is alive: ' + videoIsAlive)
videoReceivedBytetCount = report.bytesReceived;
}
if(audioIsAlive || videoIsAlive){ //either audio or video is being recieved.
log('Either video or audio is alive')
isPermanentDisconnectFlag = false; //Disconnected is temp
} else {
isPermanentDisconnectFlag = true;
}
}
})
});
return isPermanentDisconnectFlag;
}

我认为我面临的问题是由于myPeerConnection.getStats(null).then(stats => { stats.forEach(report => {..

它必须具有await,如上面的函数所示。

然后调用以上函数。请注意asyncawait

async function checkStatePermanent (iceState) {
videoReceivedBytetCount = 0;
audioReceivedByteCount = 0;
log('after First call')
let firstFlag = await isPermanentDisconnect();
log('Disconnect Flag is: ' + firstFlag)
log('after First call')
await customdelay(2000);
log('after 2 secs')
let secondFlag = await isPermanentDisconnect(); //Call this func again after 2 seconds to check whether data is still coming in.
log('after second call')
if(secondFlag){ //If permanent disconnect then we hangup i.e no audio/video is fllowing
log('Disconnect Flag = Permanent')
if (iceState == 'disconnected'){
hangUpCall(); 
}
}
..
}

日志按预期打印,现在秩序似乎得到了维护。

谢谢大家。

最新更新