Skip to content

Commit

Permalink
Added opts parameter to reconnect().
Browse files Browse the repository at this point in the history
That includes `incomingStore` and `outgoingStore` similar to
`connect()`.
If stores passed, `reconnect()` uses them, otherwise renew default `Store`.
  • Loading branch information
redboltz committed Dec 6, 2017
1 parent e271142 commit 8adeaa7
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 74 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,6 @@ After this function is called, the messageId is released and becomes reusable.
### mqtt.Client#reconnect()

Connect again using the same options as connect()
`incomingStore` and `outgoingStore` need to be ready before `reconnect()` calling.

-------------------------------------------------------
<a name="handleMessage"></a>
Expand Down
16 changes: 14 additions & 2 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -637,15 +637,27 @@ MqttClient.prototype.removeOutgoingMessage = function (mid) {

/**
* reconnect - connect again using the same options as connect()
* `incomingStore` and `outgoingStore` need to be ready before `reconnect()` calling.
*
* @param {Object} [opts] - reconnect options, includes:
* {Object} incomingStore - a store for the incoming packets
* {Object} outgoingStore - a store for the outgoing packets
* if opts is not given, current stores are used
* @returns {MqttClient} this - for chaining
*
* @api public
*/
MqttClient.prototype.reconnect = function () {
MqttClient.prototype.reconnect = function (opts) {
var that = this
var f = function () {
if (opts) {
that.options.incomingStore = opts.incomingStore
that.options.outgoingStore = opts.outgoingStore
} else {
that.options.incomingStore = null
that.options.outgoingStore = null
}
that.incomingStore = that.options.incomingStore || new Store()
that.outgoingStore = that.options.outgoingStore || new Store()
that.disconnecting = false
that.disconnected = false
that._deferredReconnect = null
Expand Down
80 changes: 10 additions & 70 deletions test/abstract_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1975,6 +1975,8 @@ module.exports = function (server, config) {
it('should preserved incomingStore after disconnecting if clean is false', function (done) {
var reconnect = false
var client = {}
var incomingStore = new mqtt.Store({ clean: false })
var outgoingStore = new mqtt.Store({ clean: false })
var server2 = new Server(function (c) {
c.on('connect', function (packet) {
c.connack({returnCode: 0})
Expand All @@ -1993,7 +1995,10 @@ module.exports = function (server, config) {
})
c.on('pubrec', function (packet) {
client.end(false, function () {
client.reconnect()
client.reconnect({
incomingStore: incomingStore,
outgoingStore: outgoingStore
})
})
})
c.on('pubcomp', function (packet) {
Expand All @@ -2010,8 +2015,8 @@ module.exports = function (server, config) {
clean: false,
clientId: 'cid1',
reconnectPeriod: 0,
incomingStore: new mqtt.Store({ clean: false }),
outgoingStore: new mqtt.Store({ clean: false })
incomingStore: incomingStore,
outgoingStore: outgoingStore
})

client.on('connect', function () {
Expand All @@ -2029,13 +2034,7 @@ module.exports = function (server, config) {
})

it('should be able to pub/sub if reconnect() is called at close handler', function (done) {
var client = connect({
clean: false,
clientId: 'cid1',
reconnectPeriod: 0,
incomingStore: new mqtt.Store({ clean: false }),
outgoingStore: new mqtt.Store({ clean: false })
})
var client = connect({ reconnectPeriod: 0 })
var tryReconnect = true
var reconnectEvent = false

Expand Down Expand Up @@ -2065,13 +2064,7 @@ module.exports = function (server, config) {
})

it('should be able to pub/sub if reconnect() is called at out of close handler', function (done) {
var client = connect({
clean: false,
clientId: 'cid1',
reconnectPeriod: 0,
incomingStore: new mqtt.Store({ clean: false }),
outgoingStore: new mqtt.Store({ clean: false })
})
var client = connect({ reconnectPeriod: 0 })
var tryReconnect = true
var reconnectEvent = false

Expand Down Expand Up @@ -2102,59 +2095,6 @@ module.exports = function (server, config) {
})
})

it('should be able to pub/sub if connect() is called at close handler', function (done) {
var client = connect({ reconnectPeriod: 0 })
var reconnectEvent = false
client.on('close', function () {
client = connect({ reconnectPeriod: 0 })
client.on('connect', function () {
client.subscribe('hello', function () {
client.end()
})
})
client.on('close', function () {
reconnectEvent.should.equal(false)
done()
})
})

client.on('reconnect', function () {
reconnectEvent = true
})

client.on('connect', function () {
client.end()
})
})

it('should be able to pub/sub if connect() is called at out of close handler', function (done) {
var client = connect({ reconnectPeriod: 0 })
var reconnectEvent = false

client.on('close', function () {
setTimeout(function () {
client = connect({ reconnectPeriod: 0 })
client.on('connect', function () {
client.subscribe('hello', function () {
client.end()
})
})
client.on('close', function () {
reconnectEvent.should.equal(false)
done()
})
}, 100)
})

client.on('reconnect', function () {
reconnectEvent = true
})

client.on('connect', function () {
client.end()
})
})

context('with alternate server client', function () {
var cachedClientListeners

Expand Down
6 changes: 5 additions & 1 deletion types/lib/client.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,11 @@ export declare class MqttClient extends events.EventEmitter {

/**
* reconnect - connect again using the same options as connect()
* `incomingStore` and `outgoingStore` need to be ready before `reconnect()` calling.
*
* @param {Object} [opts] - reconnect options, includes:
* {Object} incomingStore - a store for the incoming packets
* {Object} outgoingStore - a store for the outgoing packets
* if opts is not given, current stores are used
*
* @returns {MqttClient} this - for chaining
*
Expand Down

0 comments on commit 8adeaa7

Please sign in to comment.