Node.js quick server returns socket error, looking for correct parameters

447 views Asked by At

I am trying to use a server for quic using node version v8.4.0: git clone https://github.com/toajs/quic.git

There is no example or description for how to use the code. Here is my code saved in "test.js" to attempt to call the server:

// server
var s = require('./server');
var t = new s.Server();

t.listen(3000, 'localhost');

Here is the output of the attempted execution:

$ node test.js (node:14446) UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 1): Error [ERR_SOCKET_BAD_TYPE]: Bad socket type specified. Valid types are: udp4, udp6 (node:14446) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Here is the server.js file I'm trying to use (from the git repo):

'use strict'
// **Github:** https://github.com/toajs/quic
//
// **License:** MIT

const { createSocket } = require('dgram')
const EventEmitter = require('events')
const { lookup } = require('./common')

// Event: 'listening'
// Event: 'connection'

class Server extends EventEmitter {
  constructor () {
    super()
    this._udp = null
    this.localFamily = ''
    this.localAddress = ''
    this.localPort = 0
    this.listening = false
    this.conns = new Map()
  }

  address () {
    return {port: this.localPort, family: this.localFamily, address: this.localAddress}
  }

  async listen (port, address) {
    if (this._udp) throw new Error('Server listening')

    let type = 'upd4'
    if (address) {
      let addr = await lookup(address || 'localhost')
      if (addr.family === 6) type = 'upd6'
    }

    this._udp = createSocket(type)
    this._udp
      .on('error', (err) => this.emit('error', err))
      .on('close', () => this._onclose())
      .on('message', (msg, rinfo) => this._onmessage(msg, rinfo))

    let res = new Promise((resolve, reject) => {
      this._udp.once('listening', () => {
        this._udp.removeListener('error', reject)

        let addr = this._udp.address()
        this.localFamily = addr.family
        this.localAddress = addr.address
        this.localPort = addr.port
        this.emit('listening')
        resolve()
      })
      this._udp.once('error', reject)
    })
    // Can't support cluster
    this._udp.bind({port: port, address: address, exclusive: true})
    return res
  }

  _onmessage (msg, rinfo) {}
  _onclose () {}

  close () {}
  getConnections () {
    return Promise.resolve(this.conns.size)
  }
  ref () {}
  unref () {}
}

exports.Server = Server

Clearly I have to set the constructor socket family such as udp4 and possibly other parameters but I am not sure how to do this using the Server constructor. I've been reading about Node.js constructors but so far have not determined the right approach. Tutorials on socket usage in Node.js don't transfer to this type of code. If anyone can point me in the right direction, either for the parameter passing, or for a quic server tutorial I'd really appreciate it.

0

There are 0 answers