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