PHP和Twilio实时?
我目前正在研究一个实时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");
}
});
}
}
Twilio开发者传道这里。
我不建议轮询Twilio API以获取“实时”SMS消息。轮询效率低下,并且会使服务器和Twilio的服务器保持忙碌状态的时间超过必要的时间。
相反,我会使用Twilio的webhooks to receive messages。然后,当你在页面上收到的消息我会实现无论是服务器发送的事件(see this article for an in depth description of SSE as well as example PHP code to implement)或网络套接字(这里的一对web sockets in PHP和文章library built as part of it),以推动您从网络挂接到您的网页接收新邮件。
让我知道这是否有帮助。
谢谢。我将深入这篇文章,看看我能想出什么。 :) –
太棒了,希望它顺利! – philnash
代码审查请求在这里是无关紧要的。这些问题有适当的[SE site](http://codereview.stackexchange.com/)。 – hindmost