我目前正在研究一个实时SMS系统,但我相当积极,我目前的实施是一个可怕的做法,我正在寻找更有效的指导。PHP和Twilio实时?
的尝试
目前,当你加载它拉一切为您选择该号码的短信界面。然后,它每隔5秒向我写的Twilio JSON PHP脚本发出一个ajax调用请求比列表中最后一条消息更新的消息。
$.getJSON("/includes/twilio.php",{action:"getconvo",cid:customer.customer_number},function(data){
$('#sms_messages').html("<div></div>");
$(data.messages).each(function(){
insertSMS(this.msg,this.date,this.from);
lastMessage = this.date;
});
$("#sms_messages").animate({ scrollTop: $('#sms_messages > div').height()},"fast");
shouldUpdate = true;
sms_interval = setInterval(function(){updateSMS(customer.customer_number)},5000);
});
更新功能
function updateSMS(cid){
if(shouldUpdate){
$.getJSON("/includes/twilio.php",{action:"getconvo",cid:cid,date:lastMessage},function(data){
if(data.messages.length > 0){
// Play an embeded sound effect when a new message is found.
$('#sms_sound')[0].play();
$(data.messages).each(function(){
insertSMS(this.msg,this.date,this.from);
lastMessage = this.date;
});
$("#sms_messages").animate({ scrollTop: $('#sms_messages > div').height()},"fast");
}
});
}
}
代码审查请求在这里是无关紧要的。这些问题有适当的[SE site](http://codereview.stackexchange.com/)。 – hindmost