【发布时间】:2011-06-02 23:44:13
【问题描述】:
我正在 Node.js 上创建一个 Express JS 应用程序,并实现了一个页面,该页面将根据 chat.nodejs.org (https://github.com/ry/node_chat) 回调实时更新。我已经按照 'node_chat' 中的方法对 t 进行了操作,如果从另一个浏览器进行更新,则页面会正确更新。唯一的问题是我似乎无法阻止浏览器(Chrome)“等待本地主机”并旋转,我认为它不需要这样做。
这是我的代码的简化版本(为简洁起见)。有什么想法吗?
client.js(客户端):
function poll(data) {
// ...
if(data){
// do something with the data
}
$.ajax({
cache:false,
type:'GET',
url:'/json-people',
dataType: 'json',
data: {
slug: queue.slug,
since: CONFIG.last_timestamp
},
error: function () {
// handle error
},
success: function (data) {
poll(data);
}
});
};
app.js:
app.get('/json-people', function (req, res) {
queueProvider.query(req.param('slug'), req.param('since'), function (people){
res.send({people: people}, 200);
});
});
我在 queueProvider.query 方法中放什么似乎并不重要,它会旋转直到 res.send(...) 被执行。
我用https://github.com/ry/node_chat/blob/master/client.js 和https://github.com/ry/node_chat/blob/master/server.js 供参考。
特别是:
fu.get("/recv", function (req, res) {...}
和
channel.query()
和
channel.appendMessage()
谢谢。
【问题讨论】:
-
这是有意的。它旋转,因为它正在加载。使用 websocket。