node.js eventEmitter + http.request

有些话、适合烂在心里 提交于 2019-12-05 00:11:40

问题


i did this tutorial node.js eventEmitter, it worked nicely. I added a method that uses http.request to get data, which works and emit the data.

the problem is that the listener doesn't catch the event !

can someone help ?

code :

var events = require('events');
var util = require('util');
var http = require('http');

//http request options, it query the twitter api and get the public timeline, works!
var options = {
    hostname : 'api.twitter.com',
    port : 80,
    method : 'get',
    path : '/1/statuses/public_timeline.json?count=3&include_entities=true'
}

// The Thing That Emits Event
Eventer = function(){
  events.EventEmitter.call(this);

//tutorial examples
  this.kapow = function(){
    var data = "BATMAN"
    this.emit('blamo', data);
  }

//tutorial examples
  this.bam = function(){
     this.emit("boom");
  }

//my method
  this.GetTweetList = function(){
    var tweets = "";
        var req = http.request(options, function(response){
                    var body = "";
                    response.on('data',function(data){
                        body += data;
                    });
                    response.on('end', function(){
                        tweets = JSON.parse(body);
                        this.emit("tweets", tweets);
                        util.puts('!!!!!!!!!! got some data !!!!!!!!!! \n');
                    });
                });
        req.end();
    }
 };
util.inherits(Eventer, events.EventEmitter);

// The thing that listens to, and handles, those events
Listener = function(){

//tutorial examples
this.blamoHandler =  function(data){
    console.log("** blamo event handled");
    console.log(data);
  },

//tutorial examples
  this.boomHandler = function(data){
    console.log("** boom event handled");
  }

//my listener method
  this.GetTweetListHandler = function(data){
        console.log("** tweets event handled");
        util.put(data);
        util.puts('!!!!!!!!!! got some data in listener !!!!!!!!!! \n');

    }

};

// The thing that drives the two.
//instanciating the object and liking the methodes
var eventer = new Eventer();
var listener = new Listener(eventer);
eventer.on('blamo', listener.blamoHandler);
eventer.on('boom', listener.boomHandler);
eventer.on('tweets', listener.GetTweetListHandler);


//calling the methodes
eventer.kapow();//works
eventer.bam();//works
setInterval(eventer.GetTweetList, 2000);
//eventer.GetTweetList();// still waiting but the eventer display that he got the data

回答1:


Hard one to spot ...

The problem is the this pointer from this.emit("tweets", tweets);. You are doing this call from within an anonymous callback passed to response.on, so this does not represent the Eventer object that you created. To solve it, you need to "save" the this pointer (a common practice).

var tweets = "";
var self = this;
....
self.emit("tweets", tweets);


来源:https://stackoverflow.com/questions/10271336/node-js-eventemitter-http-request

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!