Wjdavis5 Wjdavis5 - 5 months ago 29
Javascript Question

Assign function to event

I have a class with a function that I want to assign to an event for a dgram obj.

MessageServer.prototype.OnBind = function (datagram) {
var address = datagram.address();
console.log("server listening " + address.address + ":" + address.port);
};

datagram.on("listening",
function() {
OnBind(this);
});


I've tried:


  • The above, which OnBind is not defined

  • this.OnBind, which is not defined b/c this refers to the dgram

  • MessageServer.OnBind, which is not a function

  • datagram.on("listening",this.OnBind); but that doesnt pass in the dgram

  • datagram.on("listening",this.OnBind(this)); but that passes in the MessageServer



I'm guessing this is simple and I'm just being difficult... How do I assign the function to that event handler?

EDIT - THE FULL MODULE

var Dgram = require("dgram");
var MessageQueue = require("./MessageQueue");
var Message = require("./Message");

function MessageServer(name, listenPort, messageQueue, bindCallBack, messageReceiveCallBack,closeCallback,errorCallback) {
this.Name = name;
this.MessageQueue = messageQueue;
this.Port = listenPort;
var datagram = Dgram.createSocket("udp4");
MessageServer.prototype.Bind = function() {
datagram.bind(this.Port);
};
MessageServer.prototype.OnReceive = function(message, info) {
var msg = new Message(message, info.address, info.port);
MessageQueue.Add(msg);
};

MessageServer.prototype.OnBind = function (datagram) {
var address = datagram.address();
console.log("server listening " + address.address + ":" + address.port);
};

datagram.on("listening",this.OnBind(datagram));
datagram.on("message",
function(msg,info) {
OnReceive(msg, info);
});
datagram.on("error", function(err) {
console.log(this.Name + " error: " + error);
});
datagram.on("close", function() {
console.log("server closing: " + Name);
});
}

module.exports = MessageServer;

Answer

Several issues with your code, Cleaned it up a little using the docs as reference.
See here: https://nodejs.org/api/dgram.html

Entirely off the cuff and untested off course. But it should give you an idea where to start I think.

MessageServer is a class here, Implement your logic in it's methods. Then instantiate it and call Bind to start it.
( Probably some room for improvement in this interface tough )

var myMessageServer = new MessageServer('MyServer', 8888, new MessageQueue(), etc...);
myMessageServer.Bind();

-

var Dgram = require("dgram");
var Message = require("./Message");

function MessageServer(name, listenPort, messageQueue, bindCallBack, messageReceiveCallBack, closeCallback, errorCallback) {
    this.name = name;
    this.messageQueue = messageQueue;
    this.port = listenPort;
    this.server = Dgram.createSocket("udp4");

    this.server.on("listening", this.OnBind.bind(this));
    this.server.on("message", this.OnReceive.bind(this));
    this.server.on("error", function (err) {
        console.log(this.name + " error: " + error);
        this.server.close();
    }.bind(this));
    this.server.on("close", function () {
        console.log("server closing: " + name);
    });
}

MessageServer.prototype.Bind = function () {
    this.server.bind(this.port);
};
MessageServer.prototype.OnReceive = function (message, info) {
    var msg = new Message(message, info.address, info.port);
    this.messageQueue.Add(msg);
};

MessageServer.prototype.OnBind = function () {
    var address = this.server.address();
    console.log("server listening " + address.address + ":" + address.port);
};

module.exports = MessageServer;
Comments