Skip to content

Commit

Permalink
Merge pull request #75 from renatoc/master
Browse files Browse the repository at this point in the history
Emit an event when the server receives a ping
  • Loading branch information
mcollina committed Oct 25, 2016
2 parents 8ad3734 + 9536f9a commit 9771fd7
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 7 deletions.
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ Events:
* `ack`: when a packet published to a client is delivered successfully with QoS 1 or QoS 2, arguments:
1. `packet`
2. `client`
* `ping`: when a [Client](#client) sends a ping, arguments:
1. `packet`
2. `client`
* `subscribe`: when a client sends a SUBSCRIBE, arguments:
1. `subscriptions`, as defined in the `subscriptions` property of the
[SUBSCRIBE](https://github.com/mqttjs/mqtt-packet#subscribe)
Expand Down Expand Up @@ -188,9 +191,9 @@ instance.authenticate = function (client, username, password, callback) {
```
The return code values and their responses which can be passed are given below :-

* `1` - Unacceptable protocol version
* `2` - Identifier rejected
* `3` - Server unavailable
* `1` - Unacceptable protocol version
* `2` - Identifier rejected
* `3` - Server unavailable
* `4` - Bad user name or password

-------------------------------------------------------
Expand Down
6 changes: 2 additions & 4 deletions lib/handlers/index.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict'

var write = require('../write')
var handleConnect = require('./connect')
var handleSubscribe = require('./subscribe')
var handleUnsubscribe = require('./unsubscribe')
var handlePublish = require('./publish')
var handlePuback = require('./puback')
var handlePubrel = require('./pubrel')
var handlePubrec = require('./pubrec')
var handlePing = require('./ping')

function handle (client, packet, done) {
if (packet.cmd !== 'connect' && !client.connected) {
Expand Down Expand Up @@ -43,9 +43,7 @@ function handle (client, packet, done) {
handlePubrec(client, packet, done)
break
case 'pingreq':
write(client, {
cmd: 'pingresp'
}, done)
handlePing(client, packet, done)
break
case 'disconnect':
client.disconnected = true
Expand Down
12 changes: 12 additions & 0 deletions lib/handlers/ping.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
'use strict'
var write = require('../write')

function handlePing (client, packet, done) {
client.broker.emit('ping', packet, client)
write(client, {
cmd: 'pingresp'
}, done)
done()
}

module.exports = handlePing
24 changes: 24 additions & 0 deletions test/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,27 @@ test('does not store $SYS topics to QoS 1 # subscription', function (t) {
})
})
})

test('Emit event when receives a ping', function (t) {
t.plan(6)
t.timeoutAfter(2000)
var broker = aedes()

broker.on('ping', function (packet, client) {
if (client && client) {
t.equal(client.id, 'abcde')
t.equal(packet.cmd, 'pingreq')
t.equal(packet.payload, null)
t.equal(packet.topic, null)
t.equal(packet.length, 0)
broker.close()
t.pass('ended')
}
})

var s = connect(setup(broker), { clientId: 'abcde' })

s.inStream.write({
cmd: 'pingreq'
})
})

0 comments on commit 9771fd7

Please sign in to comment.