How could I check a list of websocket servers?

I'm trying to make a system to ping a list of websocket servers, in order to check if they're online and have free slots. Using the following code, I can ping one server:

Code:
Somevariable = new WebSocket("ws://IP:port")
Somevariable.onopen=function(){
Somevariable.send("ping") //Sends the message "ping" after connecting

}

Somevariable.onmessage=function(message){
if(message==="online"){
//If the server answers with "online", the IP is added to an array that will be displayed afterwards
}
}

But how could I ping a list of servers? Because using the code above for an array of servers to ping won't work, I assume.

I need a system like this to make a correct list of servers the player can join in my game(The game downloads a .txt file with a list of servers, then the client pings all of them and list those who answered in under 5 seconds).
I don't see why you could not use arrays for this. Without testing it, I think this should work:

Code:
Somearray.forEach((ip) => {
  Somevariable = new WebSocket(`ws://${ip}:port`)
  Somevariable.onopen=function(){
    Somevariable.send("ping") //Sends the message "ping" after connecting
  }

  Somevariable.onmessage=function(message){
    if(message==="online"){
      //If the server answers with "online", the IP is added to an array that will be displayed afterwards
    }
  }
});

Where Somearray is an array containing all the IP addresses. This assumes you use a Maker version which supports ES6. It's doable in ES5 as well (ofc), but then the code is tiny bit more bloated.
I don't see why you could not use arrays for this. Without testing it, I think this should work:

Code:
Somearray.forEach((ip) => {
  Somevariable = new WebSocket(`ws://${ip}:port`)
  Somevariable.onopen=function(){
    Somevariable.send("ping") //Sends the message "ping" after connecting
  }

  Somevariable.onmessage=function(message){
    if(message==="online"){
      //If the server answers with "online", the IP is added to an array that will be displayed afterwards
    }
  }
});

Where Somearray is an array containing all the IP addresses. This assumes you use a Maker version which supports ES6. It's doable in ES5 as well (ofc), but then the code is tiny bit more bloated.
Because If I do, I will probably be setting "Somevariable" to be a connection only to the last array value in this forEach loop, not letting it finish the one before first.
I tried using this system before(was a for loop though) and it didn't work as intended.

But I think I found the solution(I'll just double check it before asking to close the topic): Making an "ArrayTester" function, which will call itself after every atempt(no matter if worked or not) to connect to a server listed in the array. Also it will use a global variable that will if check the number of times it ran isn't bigger than the array itself.
Oh right, you re-use the variable every pass through the loop. That's easily fixed, though:

Code:
let Somevariable = new WebSocket(`ws://${ip}:port`)

Adding that simple let creates a new variable each time you pass through the loop. You can not reference Somevariable from outside the loop then, but I think that's not necessary. After all, you just rely on the callbacks to write stuff in some list. If your ArrayTester function works, however, then of course you can also use that. 🙂

This thread is being closed, due to being solved. If for some reason you would like this thread re-opened, please report this post and leave a message why. Thank you.

Original thread: https://forums.rpgmakerweb.com/threads/how-could-i-check-a-list-of-websocket-servers.105934/