Home > OS >  Node.js call parent method in class
Node.js call parent method in class

Time:08-06

I am trying to emit an event inside a class.

But I can't find a way to call the parent method.

Please see the comment lines to understand what I'm trying to do.

This is my VirtualChart.js

var WebSocketClient = require('websocket').client;
const EventEmitter = require("events");

class VirtualChart extends EventEmitter {

    constructor() {

        super();

        this.client = new WebSocketClient();

        this.client.on('connectFailed', function(error) {
            console.log('Connect Error: '   error.toString());
        });

        this.client.on('connect', function(connection) {

            console.log('WebSocket Client Connected');

            connection.on('error', function(error) {
                console.log("Connection Error: "   error.toString());
            });

            connection.on('close', function() {
                console.log('echo-protocol Connection Closed');
            });

            connection.on('message', function(message) {
                if (message.type === 'utf8') {
                    console.log("Received: '"   message.utf8Data   "'");
                    this.emit("newMessage", message.utf8Data);//here is the problem
                    //this.parent.emit("newMessage", message.utf8Data);??????
                    //this.parent.parent.emit("newMessage", message.utf8Data);??????
                }
            });
        });

        this.client.connect('wss://myurl.com');

    }

}

And this is my main.js

var VirtualChart = require('./VirtualChart');

var virtualChart = new VirtualChart();

virtualChart.on("newMessage", (param) => {

    console.log("newMessage "   param);

});

CodePudding user response:

Well, I do see one problem. The value of this in this.emit() is probably wrong because it is set by the connection.on() handler and will not point to your object. You can fix that my using an arrow function for your callback instead of a plain function.

Change this:

    this.client.on('connect', function(connection) {

        console.log('WebSocket Client Connected');

        connection.on('error', function(error) {
            console.log("Connection Error: "   error.toString());
        });

        connection.on('close', function() {
            console.log('echo-protocol Connection Closed');
        });

        connection.on('message', function(message) {
            if (message.type === 'utf8') {
                console.log("Received: '"   message.utf8Data   "'");
                this.emit("newMessage", message.utf8Data);
            }
        });
    });

to this (which uses arrow functions for the callbacks):

    this.client.on('connect', connection => {

        console.log('WebSocket Client Connected');

        connection.on('error', error => {
            console.log("Connection Error: "   error.toString());
        });

        connection.on('close', () => {
            console.log('echo-protocol Connection Closed');
        });

        connection.on('message', message => {
            if (message.type === 'utf8') {
                console.log("Received: '"   message.utf8Data   "'");
                this.emit("newMessage", message.utf8Data);
            }
        });
    });

The arrow functions will preserve the lexical value of this. The regular function(...) callbacks will not and the value of this will be set by the caller of the callback (which knows nothing about your object).

  • Related