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

159
Visualizações
How to receive data on two different ports of websockets in Javascript?

I've currently initialized two different websockets with different ports like this:

const socket_ch1 = new WebSocket(WEB_socket_URL_CH1)
const socket_ch2 = new WebSocket(WEB_socket_URL_CH2)

socket_ch1.onopen = (event) => {
  console.warn('websocket_ch1 connection established', event)
}

socket_ch2.onopen = (event) => {
  console.warn('websocket_ch2 connection established', event)
}

Then, I've pushed data to a logic I've implemented upon incoming data like this.

socket_ch1.onmessage = async (event) => {
  console.log( 'onmessage Ch1: ' + flag)

  const parsedMessage = JSON.parse(event.data)
  const keys = Object.keys(parsedMessage)

  prpdch1.streamPush(parsedMessage[keys][0])
  prpsch1.streamPush(parsedMessage[keys][0])
}

socket_ch2.onmessage = async (event) => {
  console.log( 'onmessage Ch2: ' + flag)

  const parsedMessage = JSON.parse(event.data)
  const keys = Object.keys(parsedMessage)
  prpsch2.streamPush(parsedMessage[keys][0])
  prpdch2.streamPush(parsedMessage[keys][0])
}

streamPush() logic has been implemented as such :

streamPush( samples ){
    console.log('prps streamPush : ' + this.name)
    console.log(samples)


    if(samples === undefined) return

    let temp = []

    // 7680 >> [128]
    while(samples.length > 0 ) {
        var cycle = samples.splice(0, this.dataSampleSize)
        temp.push(cycle)
    }


    if(temp.length > 0)
        this.databuf = this.databuf.concat(temp)

}

However, it seems data only come through one channel, and I don't know why. The following is the debugging console.

enter image description here

I think there is a problem with how I'm using the websocket. Can anyone spot the problem?

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
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