我有一个类,其中有一个函数要分配给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);
    });

我试过:
上面的,其中onbind没有定义
this.onbind,未定义b/c这是指dgram
不是函数的MessageServer.onBind
datagram.on(“监听”,this.onbind);但这不会传入dgram
datagram.on(“监听”,this.onbind(this));但这会传入消息服务器
我想这很简单,我只是有点难…如何将函数分配给该事件处理程序?
编辑-完整模块
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;

最佳答案

您的代码有几个问题,使用文档作为参考稍微清理了一下。
请看这里:https://nodejs.org/api/dgram.html
完全是即兴表演,没有经过测试。但它应该让你知道从哪里开始。
MessageServer是一个类,在它的方法中实现逻辑。然后实例化它并调用Bind来启动它。
(在这个界面上可能还有一些改进的余地)

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;

07-24 15:12