schroffl / teamspeak-query

Teamspeak-ServerQuery library
https://www.npmjs.com/package/teamspeak-query
MIT License
18 stars 8 forks source link

Getting free port on server #5

Closed frct1 closed 6 years ago

frct1 commented 6 years ago

Hi, it`s me again. Have a question: how get free port by the loop (for) with ports range and if we get free port then do server install. Code for example

for (let i = 10020; i <= 10050; i++) {
  instance.send('use', '-virtual', { port: i }).catch(function(err) {
    if (err.msg == 'invalid serverID') {
      data.server.virtualserver_port = i;
      createServer(data.server);
      /*then we must break loop, but i don`t know how to do this. Already tried try+catch but it is not working
       * getting this err (UnhandledPromiseRejectionWarning)*/
    }
  })
}

I am haven't any ideas how realize this.

Edit (schroffl): Code Formatting

schroffl commented 6 years ago

Normally you would use the break keyword to jump out of a for- or while-loop, but it won't work in this case. The error-callback will, due to JavaScripts asynchronous nature, most certainly only get called once the for-loop is long done.

Note: I don't have time right now, I will extend this comment later :) Also: Look at the documentation for the serverlist command. I think it does what you actually want.

frct1 commented 6 years ago

No, this command does not help, because we need to do sorting (there may be problems with async). The most useful option is to make use and in case of an error create a server and break the loop.

schroffl commented 6 years ago

What exactly are you trying to achieve?

frct1 commented 6 years ago

So, whe have a port range for instance (from 10020 to 10050 for example) for one ts3 instance. When we need to create server on this instance => we must get unused port for server only from this range (it is need if we are using same ip for different instances, using different sq port). Lately i'm created it in php with function

public function findPort($startp, $endp)
{
try{
        for ($tport = $startp; $tport <= $endp; ++$tport) {
               if (!$this->serverGetByPort($tport)) {
                     return $tport;
            }
         }
}
catch (TeamSpeak3_Exception $e) {
    return $tport;
}
}

In php we can return unused tport (iterator) easy. But in nodejs have troubles with this.

schroffl commented 6 years ago

So you essentially want to have every port from 10020 – 10050 occupied by a virtual server?

The error (UnhandledPromiseRejectionWarning, see this stackoverflow answer) you are getting is probably happening, because your createServer function uses Promises (Presumably calling query.send) and doesn't provide a catch handler. The try-catch can't work, because that is for handling synchronous errors. If you want to learn more about Promises: I found this guide to be quite good in explaining the basic properties and use cases of them.

Edit: Added link to stackoverflow answer.

schroffl commented 6 years ago

Any updates? Can I close the issue?

frct1 commented 6 years ago

Hi, currently learning the art of promises :) Yes, we can close this thread. Thanks for the docs.

schroffl commented 6 years ago

Interesting topic. Good luck and have fun ;)