Skip to content

Commit b491fb5

Browse files
committed
Make reconnectAttemptsMax optional. Add maxReconnectGapInSeconds property
1 parent 7e7c291 commit b491fb5

File tree

1 file changed

+17
-12
lines changed

1 file changed

+17
-12
lines changed

Source/PusherConnection.swift

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,9 @@ public class PusherConnection {
2222
public var userDataFetcher: (() -> PusherUserData)?
2323
public var debugLogger: ((String) -> ())?
2424
public weak var stateChangeDelegate: ConnectionStateChangeDelegate?
25-
public var reconnectAttemptsMax: Int = 6
25+
public var reconnectAttemptsMax: Int? = 6
2626
public var reconnectAttempts: Int = 0
27+
public var maxReconnectGapInSeconds: Int? = nil
2728
private var reconnectTimer: NSTimer? = nil
2829
internal var reconnectOperation: NSOperation?
2930

@@ -669,17 +670,21 @@ public class PusherConnection {
669670
Attempt to reconnect triggered by a disconnection
670671
*/
671672
@objc internal func attemptReconnect() {
672-
if reconnectAttempts < reconnectAttemptsMax && connectionState != .Connected {
673-
connect()
674-
reconnectAttempts += 1
675-
let timeInterval = Double(reconnectAttempts * reconnectAttempts) * 2.0
676-
reconnectTimer = NSTimer.scheduledTimerWithTimeInterval(
677-
timeInterval,
678-
target: self,
679-
selector: #selector(attemptReconnect),
680-
userInfo: nil,
681-
repeats: false
682-
)
673+
if connectionState != .Connected {
674+
if (reconnectAttemptsMax == nil || (reconnectAttemptsMax != nil && reconnectAttempts < reconnectAttemptsMax)) {
675+
connect()
676+
reconnectAttempts += 1
677+
let reconnectInterval = Double(reconnectAttempts * reconnectAttempts) * 2.0
678+
let timeInterval = maxReconnectGapInSeconds != nil ? max(reconnectInterval, Double(maxReconnectGapInSeconds!))
679+
: reconnectInterval
680+
reconnectTimer = NSTimer.scheduledTimerWithTimeInterval(
681+
timeInterval,
682+
target: self,
683+
selector: #selector(attemptReconnect),
684+
userInfo: nil,
685+
repeats: false
686+
)
687+
}
683688
}
684689
}
685690
}

0 commit comments

Comments
 (0)