【发布时间】:2013-12-04 09:17:05
【问题描述】:
我想从 phonegap 应用程序中对我的服务器上的脚本进行长轮询,以检查服务消息、优惠等内容。
我在 js 中使用了这种技术:
(function poll(){
$.ajax({
url: "/php/notify.php",
success: function(results){
//do stuff here
},
dataType: 'json',
complete: poll,
timeout: 30000,
});
})();
它将每 5 分钟开始一次新的轮询(将在应用“暂停”时停止轮询以避免额外负载)
我不确定如何设置 php?我可以对其进行设置,使其不返回任何内容,只是通过脚本循环,但是一旦我决定要向应用程序发送消息,如何让它返回响应?到目前为止,我的 php 代码是:
<?php
include 'message.php';
$counter = 1;
while($counter > 0){
//if the data variable exists (from the included file) then send the message back to the app
if($message != ''){
// Break out of while loop if we have data
break;
}
}
//if we get here weve broken out the while loop, so we have a message, but make sure
if($message != ''){
// Send data back
print(json_encode($message));
}
?>
message.php 包含一个 $message 变量(数组),它通常是空白的,但是当我想要它时会包含数据。问题是,当我更新 message.php 中的 $message var 时,它不会将响应发送回应用程序,而是等到超时并且 poll() 函数再次启动。
所以我的问题是,我如何设置 php,以便我可以更新服务器上的消息并将其立即发送给任何轮询的人?
【问题讨论】:
标签: javascript php jquery cordova