Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

155
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda