seantrant
9/26/2017 - 8:55 AM

Create an event in a class and listen for it

Create an event in a class and listen for it

// class is called socket.js

//in constructor define event
constructor(args) {
  this.connect(args.url) // not part of event
    this._events = { 
      error: ()=>{}
    }
}

// create 'on' fucntion for error

on(name, func){
  this._events[name] = func
}

// trigger event, this case inside an error catch

this._events.error(err)



// on page add the socket class

let sock = socket({ url: 'ws://echo.websocket.org' })

// tell what to do if the event happens

sock.on('error', (error_description)=>{
  this.$router.push({ path: '/error' , query: {  error_description , error_code: 'Websocket Error'}})
})