I am sending strings from Matlab to Nodejs via TCPIP, where Matlab creates a local server and Nodejs acts as the client. Up to this point, everything works out just fine.
Now I want to perform a command if a specific string was sent. However, it seems as if the string, that is beeing received, is not assigned to the variable I am using for the codition for the if-statement. Find the code of the Nodejs-side below
client = require('./client');
host='localhost';
port=5000;
c = new client(host, port);
var message = c.receive();
if (message == "ping") {
console.log("received ping");
};
Code for the client object
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;
Is there any mistake in the first code block or do i need to modify "receive" in client? Thanks for any advice.