-1

I am using https://github.com/gpittarelli/node-ssq to query of a bunch of TF2 game servers to find out if they are on, and if so, how many players are inside. Once I find a server that is on and has less than 6 players in it, I want to use that server's Database ID to insert into somewhere else.

Code looks like this:

for (var i = 0;i < servers.length;i++) {

                ssq.info(""+servers[i].ip, servers[i].port, function (err, data) {

                    serverData = deepCopy(data);
                    serverError = deepCopy(err);
                });

                if (!serverError) {

                    if (serverData.numplayers < 6){
                        //its ok
                        currentServer = servers[i].id;
                        i = 99999;
                    }
                }
                else {
                    if (i == servers.length-1){
                        currentServer = 666;
                    }
                }

            }

And then right after I insert into database with https://github.com/felixge/node-mysql . If I put a console.log(serverData) in there, the info will show up in the console AFTER it inserted into the DB and did a couple other stuff.

So how do I "stop" node, or should I be looking at this / doing this differently?

user2123244
  • 99
  • 1
  • 4

2 Answers2

3

Update:

A simple solution here is to just move your if statements inside the callback function:

for (var i = 0;i < servers.length;i++) {
  ssq.info(""+servers[i].ip, servers[i].port, function (err, data) {

      serverData = deepCopy(data);
      serverError = deepCopy(err);

      // moving inside the function, so we have access to defined serverData and serverError
      if (!serverError) {

          if (serverData.numplayers < 6){
              //its ok
              currentServer = servers[i].id;
              i = 99999;
              /* add an additional function here, if necessary */
          }
      }
      else {
          if (i == servers.length-1){
              currentServer = 666;
              /* add an additional function here, if necessary */
          }
      }
  });
  // serverData and serverError are undefined outside of the function
  // because node executes these lines without waiting to see if ``ssq.info`` 
  // has finished. 
}

Any additional functions within the callback to ssq.info will have access to variables defined within that function. Do be careful with nesting too many anonymous functions.

Original (nodesque) Answer If ssq.info is an Asynchronous function (which it seem it is), Node is going to immediately execute it and move on, only dealing with the callback function (which you passed as a last parameter) when ssq.info has finished. That is why your console.log statement is going to execute immediately. This is the beauty/terror of node's asynchronous nature : )

You can use setTimeout to make Node wait, but that will hold up every other process on your server.

The better solution, in my opinion, would be to make use of Node's Event Emiters, to:

  1. watch for an event (in this case, when a player leaves a server)
  2. Check if the number of players is less than 6
  3. If so, execute your query function (using a callback)

A good primer on this is: Mixu's Node Book - Control Flow. Also, see this SO post.

Community
  • 1
  • 1
Nick Tomlin
  • 28,402
  • 11
  • 61
  • 90
  • Ok, I think I now better understand what "node never stops" means. The thing is, how do I set up an event for when a player joins or leaves the server? ssq.info basiclly queries the server to find out a bunch of stuff about it, one of which is the numplayers property. I do not have a constant monitor on the server, I just need which server is on and has less than 5 players, so that I can use it and redirect 12 players to it. – user2123244 Mar 19 '13 at 20:04
  • Okay, i've updated my answer with something that should help you, since you are not using an evented system. Hope that helps. – Nick Tomlin Mar 19 '13 at 20:26
0

You should use a callback,

connection.query('INSERT INTO table', function(err, rows, fields) {
  if (err) throw err;
     //entry should be inserted here.

});

also the http://www.sequelizejs.com/ library is a bit more matrue, it could be an implementation problem with node-mysql

j_mcnally
  • 6,928
  • 2
  • 31
  • 46