我尝试使用nodejs建立聊天并使用长轮询技术表达, 我为每个客户端保存名称和响应,以便在收到消息时结束它,下面是nodejs代码:
var q = parseQuery(req);
if (q.u){
client = {};
client.u = q.u;
client.res = res;
clients[q.u] = client;
clientsLastMessagesMap[q.u] = 0;
}
但是当客户端收到消息时,我会尝试在服务器端和浏览器中发送保存在客户端对象中的响应,我发现它从未收到:
for(var i in clients){
var messages = "";
while (clientsLastMessagesMap[clients[i].u] < messagesHistory.length){
messages += messagesHistory[clientsLastMessagesMap[clients[i].u] ] + ",";
clientsLastMessagesMap[clients[i].u]++;
}
if (messages !== ""){
console.log( clients[i].u + " should receive response ");
clients[i].res.writeHead(200, {'Content-Type': 'text/plain'});
clients[i].res.end();
}
}