我目前正在研究一個實時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