3

Very new to Node.JS/Socket.io, sort of scratching my head at implementing a callback correctly. Familiar with JS/jQuery but never quite grasped creating my own callbacks beyond jQuery's implementations.

Any who, I have a class with constructor, prototype methods etc. which works fine dealing with static data, however when I make a call out to a server via a TCP socket (using a wrapper module, of course) my function returns null before getting the actual data:

server.prototype.getStatus = function(){
var results = "";
 this.conn.sendCommand('status', function(err, response){
    if(err){console.error(err);return;}
        results = response['data'];
        /*Callback here - I'm trying to return the response message to the browser.*/
    });
return results;
 };

This function works with static data (e.g. results = 'test'), however, because I have to connect to the server to get the message I need to implement a callback function (I'm assuming...) - I know what I have here is incorrect to begin with.

For my 'server' code the following is being used:

 socket.on('get status', function(){
   io.emit('status', server.getStatus());
 });

This method also fires fine via a submit button currently. Naturally my static .html page grabs this fine:

  socket.on('status', function(msg){
    alert(msg);
    $('#status').append(msg);
  });

Because my function isn't correct I'm being returned null - I can see the response['data'] being returned through my console if I log the response, of course. Sort of clueless how I can make getStatus() return the response['data'] without blocking anything - but, basically I'm trying to return the response from sendCommand to the browser. I'm having difficulty

The third party module I'm using can be found here : https://github.com/dy-dx/node-rcon/blob/master/node-rcon.js

eric
  • 170
  • 1
  • 9
  • Nearly the same as [How to return the response from an Ajax call?](http://stackoverflow.com/q/14220321/710446), but your network communication is happening with socket.io instead of `XMLHttpRequest`. Notice how `sendCommand` takes a function as an argument? You need to make `getStatus` take a function argument, too, and call it when you have the result. – apsillers Dec 17 '14 at 16:26
  • Haha, that was actually one of the questions I read through while trying to implement a proper callback :P. – eric Dec 17 '14 at 17:12

1 Answers1

1

You just include a callback as an argument, and perhaps an error argument etc

server.prototype.getStatus = function(callback){
    this.conn.sendCommand('status', function(err, response){

        if(err) {
            callback( err, null );
        } else {
            callback( null, response['data'] );
        }

    });
};

then you use it

socket.on('get status', function(){
    server.getStatus(function(err, data) {
        if (!err) {
           io.emit('status', data);
        }
    });
});

you could even just do

server.prototype.getStatus = function(callback){
    this.conn.sendCommand('status', callback);
}

and

socket.on('get status', function(){
    server.getStatus(function(err, data) {
        if (!err) {
           io.emit('status', data['data']);
        }
    });
});
adeneo
  • 312,895
  • 29
  • 395
  • 388
  • Thanks! I actually had a block similar to to callback addition to getStatus while I was diving in, but I just couldn't understand how to actually return the data, my calls didn't contain any data. Any who, thanks again! I feel silly for not grasping this seeing it actually work :p. – eric Dec 17 '14 at 16:40
  • You're welcome! All the async stuff and callbacks in Node gets confusing for everyone, in the future you probably want to look into a promise library like Bluebird or Q, but how the callbacks work, that Node always uses an error object as the first argument etc. is useful to know anyway. – adeneo Dec 17 '14 at 16:49