我可以找到很多关于长轮询如何工作的信息(例如,这个和这个),但没有简单的例子说明如何在代码中实现这一点。
我能找到的只有cometd,它依赖DojoJS框架和一个相当复杂的服务器系统。。
基本上,我将如何使用Apache来服务请求,以及我将如何编写一个简单的脚本(例如,用PHP)来“长轮询”服务器以获取新消息?
该示例不一定是可扩展的、安全的或完整的,它只需要工作即可!
我可以找到很多关于长轮询如何工作的信息(例如,这个和这个),但没有简单的例子说明如何在代码中实现这一点。
我能找到的只有cometd,它依赖DojoJS框架和一个相当复杂的服务器系统。。
基本上,我将如何使用Apache来服务请求,以及我将如何编写一个简单的脚本(例如,用PHP)来“长轮询”服务器以获取新消息?
该示例不一定是可扩展的、安全的或完整的,它只需要工作即可!
当前回答
这是一个关于如何使用PHP&jQuery进行长时间轮询的5分钟精彩视频:http://screenr.com/SNH
代码与上面dbr的示例非常相似。
其他回答
下面是我为Inform8 Web开发的一个长轮询解决方案。基本上,重写类并实现loadData方法。当loadData返回值或操作超时时,它将打印结果并返回。
如果脚本的处理时间可能超过30秒,则可能需要将set_time_limit()调用更改为更长的时间。
Apache 2.0许可证。github上的最新版本https://github.com/ryanhend/Inform8/blob/master/Inform8-web/src/config/lib/Inform8/longpoll/LongPoller.php
Ryan
abstract class LongPoller {
protected $sleepTime = 5;
protected $timeoutTime = 30;
function __construct() {
}
function setTimeout($timeout) {
$this->timeoutTime = $timeout;
}
function setSleep($sleep) {
$this->sleepTime = $sleepTime;
}
public function run() {
$data = NULL;
$timeout = 0;
set_time_limit($this->timeoutTime + $this->sleepTime + 15);
//Query database for data
while($data == NULL && $timeout < $this->timeoutTime) {
$data = $this->loadData();
if($data == NULL){
//No new orders, flush to notify php still alive
flush();
//Wait for new Messages
sleep($this->sleepTime);
$timeout += $this->sleepTime;
}else{
echo $data;
flush();
}
}
}
protected abstract function loadData();
}
我认为客户端看起来像一个普通的异步AJAX请求,但您希望它需要“很长时间”才能返回。
然后服务器看起来像这样。
while (!hasNewData())
usleep(50);
outputNewData();
因此,AJAX请求将发送到服务器,可能包括上次更新的时间戳,以便hasNewData()知道您已经获得了哪些数据。然后,服务器在循环中休眠,直到新数据可用。一直以来,您的AJAX请求仍然连接,只是挂在那里等待数据。最后,当新数据可用时,服务器将其提供给AJAX请求并关闭连接。
最简单的NodeJS
const http = require('http');
const server = http.createServer((req, res) => {
SomeVeryLongAction(res);
});
server.on('clientError', (err, socket) => {
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});
server.listen(8000);
// the long running task - simplified to setTimeout here
// but can be async, wait from websocket service - whatever really
function SomeVeryLongAction(response) {
setTimeout(response.end, 10000);
}
在Express for exmaple的生产方面的场景中,您将在中间件中得到响应。做您需要做的事情,可以将所有长轮询方法的范围扩展到Map或其他流(对其他流可见),并在准备就绪时调用<Response>Response.end()。长轮询连接没有什么特别之处。休息只是您通常构建应用程序的方式。
如果你不知道我所说的范围界定是什么意思,这会让你明白
const http = require('http');
var responsesArray = [];
const server = http.createServer((req, res) => {
// not dealing with connection
// put it on stack (array in this case)
responsesArray.push(res);
// end this is where normal api flow ends
});
server.on('clientError', (err, socket) => {
socket.end('HTTP/1.1 400 Bad Request\r\n\r\n');
});
// and eventually when we are ready to resolve
// that if is there just to ensure you actually
// called endpoint before the timeout kicks in
function SomeVeryLongAction() {
if ( responsesArray.length ) {
let localResponse = responsesArray.shift();
localResponse.end();
}
}
// simulate some action out of endpoint flow
setTimeout(SomeVeryLongAction, 10000);
server.listen(8000);
正如你所看到的,你真的可以对所有的联系做出反应,一是做任何你想做的事。每个请求都有id,因此您应该能够使用map和访问特定的api外调用。
WS-I小组发布了一个名为“可靠的安全配置文件”的东西,它有一个GlassFish和.NET实现,显然可以很好地进行交互操作。
幸运的是,还有一个Javascript实现。
还有一个Silverlight实现使用HTTP双工。当推送发生时,可以将javascript连接到Silverlight对象以获取回调。
也有商业付费版本。
为什么不考虑web套接字而不是长轮询?它们非常高效且易于设置。然而,它们仅在现代浏览器中受支持。这里是一个快速参考。