我正在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以供参考。
Particularily:
fu.get("/recv", function (req, res) {...}
和
channel.query()
和
channel.appendMessage()
感谢。