Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
Websocket streams check connection

With the following code i open a stream connection to the Binance crypto exchange:

let adress = 'wss://stream.binance.com:9443/ws/btcusdt@kline_1h';
const ws = new WebSocket(adress);

If i make this call for different crypto currencys then i have later a few streams open, i want to know how can i check the current open streams, is there something like a function or parameter where i can see for which currencys i have a open stream running?

because i also have the problem that it looks like streams are stopping sometimes and i dont have a good solution for checking which streams have stop and how to receonnect them. My idea is now to first find a way how to check which streams are running and then maybe if one streams is stop i will just send the connection request again.

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

In javascript there is a websocket events thing, so all you need is to

ws.onclose = function(event) {
   ws = new WebSocket(adress);
    //just reopen it
};

Or, for more safety, you can

ws.onclose = function(event) {
    if (event.wasClean) {
        ws = new WebSocket(adress);
    } else {
        console.log('Connection error!');
        //or whatever u want
    }
};

Sorry for this stupid styling, I'm newbie there

about 4 years ago · Juan Pablo Isaza Relatório

0

If you have your ws variable, then checking whether the websocket is open and alive is done with

if(ws && ws.readyState === 1){
  // is opened
}

For other states of the websocket, see the docs.

about 4 years ago · Juan Pablo Isaza Relatório

0

If you want to receive push messages from the server, you need to keep the ws connection open. If not, you can close the ws after a query and reopen it then for another query. You should wait for the closed state ws.readyState === 3 before reopening.

If you need to keep all ws connections open, then you need a list of ws Objects. You push new objects to the list:

let ws_list = []  // global list of ws objects

let create_connection = function(url){
  try{
     ws_list.push(new WebSocket(url));
  } catch(err){
    console.log(err, url);
  }
}

let do_something = function(){
  for(let ws of ws_list){
    // do something with the ws object
  }
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda