Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

254
Vistas
No value returning from callback - Nodejs, Javascript

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.

over 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda