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

171
Vistas
Attach an event handler to an object literal

I have an object literal to open a websocket connection. this is a simplified version of it:

const ws = {

    conn : null,

    start : function(){
            
            this.conn = new WebSocket('wss://.....');

            this.conn.onopen = (e) => {};   

            this.conn.onmessage = (e) => {};    
                
            this.conn.onclose = (e) => {};  
                    
        }
}

i can initialize the connection with:

var websocket = ws.start();

now i want to attach an eventhandler to websocket, which is called when onmessage is fired. like this:

websocket.on('message', function (e) {

    console.log('this.conn.onmessage in ws was fired');

});

is there a way to achieve this?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Just return the connection at the end of the start function.

const ws = {

    conn : null,

    start : function(){
            
            this.conn = new WebSocket('wss://.....');

            this.conn.onopen = (e) => {};   

            this.conn.onmessage = (e) => {};    
                
            this.conn.onclose = (e) => {};  
                    
            return this.conn;
        }
}
about 4 years ago · Juan Pablo Isaza Denunciar

0

You can return the websocket at the end of the start function.

start: function() {
    this.conn = new WebSocket('wss://.....');
    this.conn.onopen = (e) => {};
    this.conn.onmessage = (e) => {};
    this.conn.onclose = (e) => {};
    return this.conn;
}
about 4 years ago · Juan Pablo Isaza Denunciar
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