Browse Source

make bind resolve the port and fix `this` bug

session-estimator
Mathias Buus 4 years ago
parent
commit
9f75648276
  1. 8
      lib/rpc.js

8
lib/rpc.js

@ -51,7 +51,7 @@ module.exports = class RPC {
bind (port = this._bind) {
if (this._binding) return this._binding
const defaultPort = this._bind
const self = this
this._binding = new Promise((resolve, reject) => {
const s = this.socket
@ -61,16 +61,16 @@ module.exports = class RPC {
s.on('error', onerror)
function onlistening () {
this._bound = true
self._bound = true
s.removeListener('listening', onlistening)
s.removeListener('error', onerror)
resolve()
resolve(s.address().port)
}
function onerror (err) {
// retry on any port if preferred port is unavail
if (port === defaultPort && port !== 0) {
if (port === self._bind && port !== 0) {
port = 0
s.bind(0)
return

Loading…
Cancel
Save