0

I am a beginner of Node.js.
Recently i would like to build a modulize system based on es6-class.
I am trying to wrap ws module to a class, and here is the prototype of my code:

const WebSocket = require('ws')

export default class WebSocketServer {
  constructor(port = 9876) {
    this.wss = new WebSocket.Server({
      port,
      perMessageDeflate: false
    })

    this.wss.on('connection', (function _connection(ws) {
      this.connection(ws)
    }).bind(this))
  }

  connection(ws) {
    return new Promise(function (resolve, reject){
      ws.on('message', function _incoming(message){
        console.log('received: %s', message)
      })

      ws.send('Welcome!', function ack(err){
        if(err) {
          reject(`Connection error: ${err}`)
        } 
        else {
          resolve('Connection created.')
        }   
      })
    })
  }
}

Few question:
1. I wonder my logic is right?
2. Is there a simple way to instead bind 'this' to callback function?

Thanks!

Ikki
  • 1
  • 3
  • `this.connection = this.connection.bind(this); this.wss.on('connection', this.connection)` ... or even just `this.wss.on('connection', this.connection.bind(this));` – Jaromanda X Apr 16 '19 at 04:00
  • @JaromandaX How can i bind other arguments into this.connection? – Ikki Apr 16 '19 at 04:04
  • how can you do what now? bind other what? – Jaromanda X Apr 16 '19 at 04:04
  • @JaromandaX bind 'ws' – Ikki Apr 16 '19 at 04:05
  • why? isn't `ws` the same as `this.wss`? – Jaromanda X Apr 16 '19 at 04:05
  • Looking at the code, I don't even understand why `connection` returns a promise - that makse no sense – Jaromanda X Apr 16 '19 at 04:06
  • @JaromandaX yes, ws = this.wss. I just want to do some sync process out of the class, and let my code clean in main js file. – Ikki Apr 16 '19 at 04:11
  • if `ws == this.wss`, just use `this.wss` inside that function – Jaromanda X Apr 16 '19 at 04:20
  • `I just want to do some sync process out of the class, and let my code clean in main js file` - what does that have to do with returning ANYTHING (not just a promise) from `connection` function ... the returned value isn't available anywhere, and `sync processing` makes zero sense when using Promises, since the very nature of Promises is to make handling of `Async` processes easier – Jaromanda X Apr 16 '19 at 04:21
  • @JaromandaX Maybe i describe not clearly. I want to use async await to let my code look like fake sync. – Ikki Apr 16 '19 at 04:41
  • right ... so, again, why is an event handler `connection` returning a promise ... the returned promise isn't anywhere you can `await` it, so why do it? – Jaromanda X Apr 16 '19 at 04:47
  • @JaromandaX Oh, i think i understand what you mean. Is there any way to modify the code to catch the promise return outside of class? – Ikki Apr 16 '19 at 04:55
  • nothing to do with "class" .. it's how event handlers i.e. `xxx.on('event', handler)` the return value in handler is not actually returned anywhere meaningful - but, that's completely nothing to do with this question, so, if you end up having issues, ask about it – Jaromanda X Apr 16 '19 at 04:57

0 Answers0