没有从回调中返回的值 - Nodejs,Javascript

时间:2017-04-26 11:49:48

标签: javascript node.js

我通过TCPIP从Matlab向Nodejs发送字符串,其中Matlab创建本地服务器,Nodejs充当客户端。到目前为止,一切正常。

现在我想在发送特定字符串时执行命令。但是,好像收到的字符串似乎没有分配给我用于if语句的编码的变量。在

下面找到Nodejs端的代码
client = require('./client');
host='localhost';
port=5000;
c = new client(host, port);

var message = c.receive();
if (message == "ping") {
    console.log("received ping");
};

客户端对象的代码

net = require('net');

function Client(host, port){
this.queue = [];
this.socket = new net.Socket();
this.socket.connect(port, host, function() {
    console.log('Connected');
});
this.queue = [];
}

Client.prototype.send = function (data){
this.socket.write(data+'\n');
}

Client.prototype.receive = function (){
var that = this;
this.socket.on('data', function(data) {
    that.queue.push(data);
    console.log(''+data);
});
}

Client.prototype.disconnect = function (){
this.socket.on('close', function() {
    console.log('Connection closed');
    this.socket.destroy();
});
}

module.exports = Client;

第一个代码块是否有任何错误,或者我是否需要在客户端修改“接收”? 谢谢你的任何建议。

0 个答案:

没有答案