基本上我有一个 db.test.message 集合,只有字段 _id 和文字
客户端将已打印的消息数发送到服务器(前0),如果 db.test.messages.count()大于此数,则仅发送新消息(或文件),如果不是,则服务器等待直到创建新消息。它一遍又一遍地重复这个操作。
首先正确打印所有邮件,但第二次请求时出错(localhost:3000 / messages?num_of_messages = 3)
Error: Can't set headers after they are sent.
at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:335:11)
at ServerResponse.writeHead (_http_server.js:195:21)
at c:\nodejs\myapp\app.js:41:30
at handleCallback (c:\nodejs\node_modules\mongodb\lib\utils.js:95:12)
at c:\nodejs\node_modules\mongodb\lib\cursor.js:590:16
at handleCallback (c:\nodejs\node_modules\mongodb\node_modules\mongodb-core\lib\cursor.js:234:5)
at setCursorDeadAndNotified (c:\nodejs\node_modules\mongodb\node_modules\mongodb-core\lib\cursor.js:424:3)
at nextFunction (c:\nodejs\node_modules\mongodb\node_modules\mongodb-core\lib\cursor.js:586:7)
at Cursor.next (c:\nodejs\node_modules\mongodb\node_modules\mongodb-core\lib\cursor.js:614:3)
at fetchDocs (c:\nodejs\node_modules\mongodb\lib\cursor.js:586:10)
我使用了这个例子并将其改为我的需要: http://www.stoimen.com/blog/2010/12/02/diving-into-node-js-a-long-polling-example/
我知道我在使用 response.end()之后尝试写 response.wirte ,但根据上面的示例和我的代码&#39 ;除非发生一些我不知道的异步执行,否则不可能发生。
这是客户端代码:
$(function () {
$('#ready').append('ready');
function check_for_new_messages(num_of_messages) {
$.ajax({
cache: false,
// setup the server address
url: '/messages?num_of_messages=' + num_of_messages,
data: {},
success: function (response, code, xhr) {
if (response.no_new_messages) {
check_for_new_messages(num_of_messages);
return false;
}
// do whatever you want with the response
var msgs = response;
for (var i = 0; i < msgs.length; i++) {
$('#messages').append(msgs[i]._id + ',' + msgs[i].text + '<br/>');
}
// make new call
check_for_new_messages(num_of_messages + msgs.length);
}
});
}
check_for_new_messages(0);
});
服务器端代码:
var http = require("http"), fs = require("fs"), url = require('url');
http.createServer(function (request, response) {
var parameters = url.parse(request.url, true).query;
var num_of_messages = parseInt(parameters.num_of_messages);
check_for_new_messages(new Date(), num_of_messages, request, response);
}).listen(80);
function check_for_new_messages(startingTime, num_of_messages, request, response) {
var date = new Date();
if (date - startingTime >= 60000) {
response.writeHead(200, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
});
response.write('{no_new_messages: true}', 'utf8');
response.end();
return false;
}
var mongoClient = require('mongodb').MongoClient;
mongoClient.connect("mongodb://localhost:27017/test", function (err, db) {
var c = db.collection('message');
c.count(function (err, count) {
if (count > num_of_messages) {
c.find({_id: {$gt: num_of_messages}}).toArray(function (err, items) {
var str_items = JSON.stringify(items);
response.writeHead(200, {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
});
response.write(str_items, 'utf8');
response.end();
return false;
});
}
});
});
setTimeout(function () {
check_for_new_messages(startingTime, num_of_messages, request, response)
}, 10000);
}
感谢您的时间。