Node.js 重复侦听器问题
我是 Node.js 的新手,很难掌握事件监听器/发射器的动态。我正在尝试为典型的“新闻源”应用程序设置一个简单的长轮询侦听器。我可以使用下面的代码向侦听器注册并向侦听器发出事件。我的问题是,每次发出事件时,它都会被多次(两次或更多次!)推送到客户端。
var http = require('http'),
events = require('events'),
url = require('url');
var event_emitter = new events.EventEmitter();
http.createServer(function(req,res) {
var uriParse = url.parse(req.url,true);
var pathname = uriParse.pathname;
console.log(req.url);
if (pathname=="//register") {
var thisRes = res;
event_emitter.addListener('event',function(actionid){
if (thisRes) {
thisRes.writeHead(200, { "Content-Type": "text/plain" });
thisRes.end(actionid);
thisRes = null;
}
});
} else if (pathname=="//emit") {
var actionid = uriParse.query.id;
event_emitter.emit('event',actionid);
console.log('event',actionid);
res.writeHead(200, { "Content-Type": "text/plain" });
res.end('success');
}
}).listen(3000,"127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');
控制台日志显示每个 ajax 请求的两个“注册”请求,我不确定 ?get 查询源自何处:
//注册?=1304280004069
//注册?=1304280004068
客户端的ajax调用(位于html正文中):
$(document).ready(function() {
function callNode() {
$.ajax({
url: '/node/register',
cache: false,
timeout: 100000 * 1000000,
success: function(data) {
function doSomethingWithData(newaction) {
$('#feed').prepend(newaction);
}
$.get('/news/'+ data+'/', doSomethingWithData);
callNode();
}
});
};
callNode();
});
最后,另一个问题是ajax调用没有在后台发生,但页面似乎正在“加载”,直到第一个节点发射(之后它会在后台默默运行)。感谢您的任何建议。
I'm new to node.js and having a hard time grasping the event listener / emitter dynamic. I'm trying to set up a simple long-polling listener for a typical 'newsfeed' app. I'm able to register and emit events to listeners with the code below. My problem is that each time an event is emitted, it is pushed to the client-side multiple (two or more!) times.
var http = require('http'),
events = require('events'),
url = require('url');
var event_emitter = new events.EventEmitter();
http.createServer(function(req,res) {
var uriParse = url.parse(req.url,true);
var pathname = uriParse.pathname;
console.log(req.url);
if (pathname=="//register") {
var thisRes = res;
event_emitter.addListener('event',function(actionid){
if (thisRes) {
thisRes.writeHead(200, { "Content-Type": "text/plain" });
thisRes.end(actionid);
thisRes = null;
}
});
} else if (pathname=="//emit") {
var actionid = uriParse.query.id;
event_emitter.emit('event',actionid);
console.log('event',actionid);
res.writeHead(200, { "Content-Type": "text/plain" });
res.end('success');
}
}).listen(3000,"127.0.0.1");
console.log('Server running at http://127.0.0.1:3000/');
The console log is showing two 'register' requests for every ajax request and I'm not sure from where the ?get queries originate:
//register?=1304280004069
//register?=1304280004068
Client's ajax call (located in html body):
$(document).ready(function() {
function callNode() {
$.ajax({
url: '/node/register',
cache: false,
timeout: 100000 * 1000000,
success: function(data) {
function doSomethingWithData(newaction) {
$('#feed').prepend(newaction);
}
$.get('/news/'+ data+'/', doSomethingWithData);
callNode();
}
});
};
callNode();
});
Finally, the other issue is that the ajax call is not occurring in the background but the page appears to be "loading" until the first node emission (after which it runs silently in the background). Thanks for any suggestions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我假设您正在使用 jQuery 来处理客户端请求。看起来,通过将 cache 设置为 false,jQuery 会向每个请求添加唯一的查询字符串,以确保您获得新数据。
您可能还希望将 doSomethingWithData 方法移出 AJAX 调用,以使其更易于阅读。
I'm assuming you're using jQuery for the client request. It looks like by setting cache to false, jQuery is adding in a unique query string to each request to make sure you get new data.
You may also want to move the doSomethingWithData method out of your AJAX call to make it a bit easier to read.