@@ -39,7 +39,7 @@ import {
39
39
} from './shared'
40
40
import TopicAliasSend from './topic-alias-send'
41
41
import { TypedEventEmitter } from './TypedEmitter'
42
- import PingTimer from './PingTimer '
42
+ import KeepaliveManager from './KeepaliveManager '
43
43
import isBrowser , { isWebWorker } from './is-browser'
44
44
45
45
const setImmediate =
@@ -433,10 +433,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
433
433
434
434
public noop : ( error ?: any ) => void
435
435
436
- /** Timestamp of last received control packet */
437
- public pingResp : number
438
-
439
- public pingTimer : PingTimer
436
+ public keepaliveManager : KeepaliveManager
440
437
441
438
/**
442
439
* The connection to the Broker. In browsers env this also have `socket` property
@@ -572,8 +569,8 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
572
569
// map of a subscribe messageId and a topic
573
570
this . messageIdToTopic = { }
574
571
575
- // Ping timer , setup in _setupPingTimer
576
- this . pingTimer = null
572
+ // Keepalive manager , setup in _setupKeepaliveManager
573
+ this . keepaliveManager = null
577
574
// Is the client connected?
578
575
this . connected = false
579
576
// Are we disconnecting?
@@ -660,7 +657,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
660
657
this . log ( 'close :: clearing connackTimer' )
661
658
clearTimeout ( this . connackTimer )
662
659
663
- this . _destroyPingTimer ( )
660
+ this . _destroyKeepaliveManager ( )
664
661
665
662
if ( this . topicAliasRecv ) {
666
663
this . topicAliasRecv . clear ( )
@@ -1780,7 +1777,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
1780
1777
this . _setupReconnect ( )
1781
1778
}
1782
1779
1783
- this . _destroyPingTimer ( )
1780
+ this . _destroyKeepaliveManager ( )
1784
1781
1785
1782
if ( done && ! this . connected ) {
1786
1783
this . log (
@@ -2064,45 +2061,36 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
2064
2061
}
2065
2062
2066
2063
/**
2067
- * _setupPingTimer - setup the ping timer
2068
- *
2069
- * @api private
2064
+ * _setupKeepaliveManager - setup the keepalive manager
2070
2065
*/
2071
- private _setupPingTimer ( ) {
2066
+ private _setupKeepaliveManager ( ) {
2072
2067
this . log (
2073
- '_setupPingTimer :: keepalive %d (seconds)' ,
2068
+ '_setupKeepaliveManager :: keepalive %d (seconds)' ,
2074
2069
this . options . keepalive ,
2075
2070
)
2076
2071
2077
- if ( ! this . pingTimer && this . options . keepalive ) {
2078
- this . pingTimer = new PingTimer (
2079
- this . options . keepalive ,
2080
- ( ) => {
2081
- this . _checkPing ( )
2082
- } ,
2072
+ if ( ! this . keepaliveManager && this . options . keepalive ) {
2073
+ this . keepaliveManager = new KeepaliveManager (
2074
+ this ,
2083
2075
this . options . timerVariant ,
2084
2076
)
2085
- this . pingResp = Date . now ( )
2086
2077
}
2087
2078
}
2088
2079
2089
- private _destroyPingTimer ( ) {
2090
- if ( this . pingTimer ) {
2091
- this . log ( '_destroyPingTimer :: destroying ping timer ' )
2092
- this . pingTimer . destroy ( )
2093
- this . pingTimer = null
2080
+ private _destroyKeepaliveManager ( ) {
2081
+ if ( this . keepaliveManager ) {
2082
+ this . log ( '_destroyKeepaliveManager :: destroying keepalive manager ' )
2083
+ this . keepaliveManager . destroy ( )
2084
+ this . keepaliveManager = null
2094
2085
}
2095
2086
}
2096
2087
2097
2088
/**
2098
-
2099
- * _shiftPingInterval - reschedule the ping interval
2100
- *
2101
- * @api private
2089
+ * Reschedule the ping interval
2102
2090
*/
2103
- private _shiftPingInterval ( ) {
2091
+ public reschedulePing ( ) {
2104
2092
if (
2105
- this . pingTimer &&
2093
+ this . keepaliveManager &&
2106
2094
this . options . keepalive &&
2107
2095
this . options . reschedulePings
2108
2096
) {
@@ -2115,34 +2103,20 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
2115
2103
*/
2116
2104
private _reschedulePing ( ) {
2117
2105
this . log ( '_reschedulePing :: rescheduling ping' )
2118
- this . pingTimer . reschedule ( )
2106
+ this . keepaliveManager . reschedule ( )
2119
2107
}
2120
2108
2121
- /**
2122
- * _checkPing - check if a pingresp has come back, and ping the server again
2123
- *
2124
- * @api private
2125
- */
2126
- private _checkPing ( ) {
2127
- this . log ( '_checkPing :: checking ping...' )
2128
- // give 100ms offset to avoid ping timeout when receiving fast responses
2129
- const timeSincePing = Date . now ( ) - this . pingResp - 100
2130
- if ( timeSincePing <= this . options . keepalive * 1000 ) {
2131
- this . log ( '_checkPing :: ping response received in time' )
2132
- this . _sendPing ( )
2133
- } else {
2134
- // do a forced cleanup since socket will be in bad shape
2135
- this . emit ( 'error' , new Error ( 'Keepalive timeout' ) )
2136
- this . log ( '_checkPing :: calling _cleanUp with force true' )
2137
- this . _cleanUp ( true )
2138
- }
2139
- }
2140
-
2141
- private _sendPing ( ) {
2109
+ public sendPing ( ) {
2142
2110
this . log ( '_sendPing :: sending pingreq' )
2143
2111
this . _sendPacket ( { cmd : 'pingreq' } )
2144
2112
}
2145
2113
2114
+ public onKeepaliveTimeout ( ) {
2115
+ this . emit ( 'error' , new Error ( 'Keepalive timeout' ) )
2116
+ this . log ( 'onKeepaliveTimeout :: calling _cleanUp with force true' )
2117
+ this . _cleanUp ( true )
2118
+ }
2119
+
2146
2120
/**
2147
2121
* _resubscribe
2148
2122
* @api private
@@ -2205,7 +2179,7 @@ export default class MqttClient extends TypedEventEmitter<MqttClientEventCallbac
2205
2179
2206
2180
this . connackPacket = packet
2207
2181
this . messageIdProvider . clear ( )
2208
- this . _setupPingTimer ( )
2182
+ this . _setupKeepaliveManager ( )
2209
2183
2210
2184
this . connected = true
2211
2185
0 commit comments