Skip to content

Commit

Permalink
Update ZpListener.js
Browse files Browse the repository at this point in the history
Code cleanup
  • Loading branch information
ebaauw committed Jun 19, 2019
1 parent 49cb035 commit 4fb531c
Showing 1 changed file with 9 additions and 16 deletions.
25 changes: 9 additions & 16 deletions lib/ZpListener.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,17 +33,17 @@ class ZpListener extends events.EventEmitter {
if (
alias.family === 'IPv4' && alias.internal === false
) {
myIps.push([alias.address, alias.netmask])
myIps.push({ address: alias.address, netmask: alias.netmask })
}
}
}
if (myIps.length === 1 && ip == null) {
return myIps[0][0]
return myIps[0].address
}
if (myIps.length > 0 && ip != null) {
for (const myIp of myIps) {
if (ZpListener._inSameNetwork(ip, myIp[0], myIp[1])) {
return myIp[0]
for (const { address, netmask } of myIps) {
if (ZpListener._inSameNetwork(ip, address, netmask)) {
return address
}
}
}
Expand Down Expand Up @@ -79,7 +79,7 @@ class ZpListener extends events.EventEmitter {
const client = this._clients[array[2]]
if (
array[1] === 'notify' && client !== null &&
array[3] != null && array[4] != null
array[3] != null && array[4] != null && array[5] === 'Event'
) {
// this._debug('%s %s event: %s', array[3], array[4], request.body)
const payload = await this._parser.parse(request.body)
Expand All @@ -95,7 +95,6 @@ class ZpListener extends events.EventEmitter {
})
this._server.on('error', (error) => { this.emit('error', error) })
this._server.on('close', () => {
// Doesn't get triggered?
this.emit('close', this._callbackUrl)
delete this._callbackUrl
})
Expand Down Expand Up @@ -133,25 +132,19 @@ class ZpListener extends events.EventEmitter {
})
}

close () {
this.emit('close', this._callbackUrl)
delete this._callbackUrl
this._server.close()
}

async addClient (zpClient) {
this._debug('addClient(%j)', zpClient)
this._clients[zpClient.id] = zpClient
await this.listen(zpClient.ip)
await this.listen(zpClient.address)
const callbackUrl = this._callbackUrl + '/' + zpClient.id
this._debug('addClient(%j) => %j', zpClient, callbackUrl)
return callbackUrl
}

async removeClient (zpClient) {
delete this._clients[zpClient._id]
delete this._clients[zpClient.id]
if (Object.keys(this._clients).length === 0) {
this.close()
this._server.close()
}
}
}
Expand Down

0 comments on commit 4fb531c

Please sign in to comment.