我为我的应用提供主要和辅助短信服务。如果主服务器出现故障,则由于任何问题,我都将默认使用辅助服务。
这是流程:
- 发送到短信服务
- 短信服务以收据确认作为响应
- 短信服务向手机运营商发送消息
- 短信服务收到电话运营商收据
- 然后我的应用可以查询短信服务以获取运营商收据
但是,由于所涉及的时间段(互联网,运营商延迟等),我需要循环#5直到运营商收据进来...或超时...以确定是否需要转到二级服务。目前,在查询#5之前,我正在执行3秒的超时延迟-但我想将其转换为1秒的循环,以确保如果#4花费的时间超过3秒,我的#5仍将正常工作。
$scope.sendCode = function() {
$scope.errColor = "red" ;
$scope.errMsg = "SENDING AUTH CODE" ;
var newCode = getRandom(10000,99999) ;
//var tktCode = new Date().format("yyyymmdd")+ "_" +getRandom(10000,999999) ;
var smsNumber = getDB("user_phone") ;
setDB("app_regCode",newCode) ;
var msg = "App registration code: "+newCode+ ". Enter this code into the 'VERIFY CODE' input field" ;
var tReq = twilio(smsNumber,msg) ;
var smsCarrier = getDB("dev_carrier") ;
if (!smsCarrier) {
smsCarrier = "No Carrier Info" ;
}
var smsEmail = $scope.userObj.user_email ;
if (!smsEmail) {
smsEmail = "No Email" ;
}
$scope.errColor ;
$scope.errMsg ;
var error = {} ;
var smsShortCode = vendorData.twilioShortCode ;
var smsLongCode = vendorData.twilioLongCode ;
// log message sent to internal API
$scope.smsID = logSMS(smsNumber,smsLongCode,smsShortCode,msg,smsCarrier,smsEmail,"Twilio") ;
$http(tReq).then(function(response) {
$scope.errColor = "green" ;
$scope.errMsg = "SENT! Confirming Delivery" ;
smsResponse = response.data ;
if (smsResponse.sid) {
//update smsSent record
apiService.all("PUT",[{table:"smsSent",sid:"rid",sidValue:$scope.smsID,soloField:1,dataInfo:{smsTkt:smsResponse.sid}}]) ;
}
if (smsResponse.status.match(/(queued|accepted|sending|sent)/i)) { //if (sms.success == true) {
// need to replace this with a proper 1 second loop
// to test for "delivered" carrier receipt
// if status still (queued|accepted|sending|sent), then loop until its updated
// final receipt statuses are: Delivered, Undelivered, Failed
setTimeout(function() {
return $http(twilioVerify(smsResponse.uri))
.then(function(response) {
// now check status of message to determine if needed to exit or go to back up service
//return $http(twilioVerify(smsResponse.uri))
if (response.data.status == "delivered") {
$scope.errColor = "green"
$scope.errMsg = "Delivery Confirmed" ;
messageSent('Primary');
} else {
//backup service
// determine fail reason, send to secondary if necessary
messageFail('Primary') ;
$scope.errColor = "red"
$scope.errMsg = "Problem detected, trying secondary service" ;
secondSMS(smsNumber,msg) ;
}
}) ;
},3000) ;
} else { // failed response from Twilio, send to MissionMobile
$scope.errColor = "red" ;
$scope.errMsg = "Problem detected, attempting secondary service"
messageFail('Primary') ;
secondarySMS(smsNumber,msg) ;
}
}, function(thisError) { // failed communication to Twilio, send to MissionMobile
$scope.errColor = "red" ;
$scope.errMsg = "Issue sending code, attempting secondary service" ;
messageFail('Primary') ;
secondarySMS(smsNumber,msg) ;
}).catch(function(error) {
$scope.errColor = error.errColor ;
$scope.errMsg = error.errMsg ;
}) ;
}
答案 0 :(得分:1)
如何使用$interval
:
final_check_interval = $interval(function() {
return $http(twilioVerify(smsResponse.uri))
.then(function(response) {
// now check status of message to determine if needed to exit or go to back up service
//return $http(twilioVerify(smsResponse.uri))
if (response.data.status == "delivered") {
$scope.errColor = "green"
$scope.errMsg = "Delivery Confirmed" ;
messageSent('Primary');
} else {
//backup service
// determine fail reason, send to secondary if necessary
messageFail('Primary') ;
$scope.errColor = "red"
$scope.errMsg = "Problem detected, trying secondary service" ;
secondSMS(smsNumber,msg) ;
}
}) ;
},1000) ;
并在messageSent()
上,确保您取消将每1秒触发一次的时间:
$interval.cancel(final_check_interval);
希望您能找到所需的线索