@@ -30,37 +30,71 @@ extension PusherConnection: WebSocketDelegate {
30
30
- parameter error: The error, if one exists, when disconnected
31
31
*/
32
32
public func websocketDidDisconnect( ws: WebSocket , error: NSError ? ) {
33
- updateConnectionState ( . Disconnected)
34
- for (_, channel) in self . channels. channels {
35
- channel. subscribed = false
33
+ // Handles setting channel subscriptions to unsubscribed wheter disconnection
34
+ // is intentional or not
35
+ if connectionState == . Disconnecting || connectionState == . Connected {
36
+ for (_, channel) in self . channels. channels {
37
+ channel. subscribed = false
38
+ }
36
39
}
37
40
38
41
// Handle error (if any)
39
42
guard let error = error where error. code != Int ( WebSocket . CloseCode. Normal. rawValue) else {
40
- return
43
+ self . debugLogger ? ( " [PUSHER DEBUG] Deliberate disconnection - skipping reconnect attempts " )
44
+ return updateConnectionState ( . Disconnected)
41
45
}
42
46
43
47
print ( " Websocket is disconnected. Error: \( error. localizedDescription) " )
48
+ // Attempt reconnect if possible
44
49
45
- // Reconnect if possible
46
- if self . options. autoReconnect && reconnectAttempts == 0 {
47
- if let reachability = self . reachability where reachability. isReachable ( ) {
48
- let operation = NSBlockOperation {
49
- self . socket. connect ( )
50
- }
50
+ guard self . options. autoReconnect else {
51
+ return updateConnectionState ( . Disconnected)
52
+ }
51
53
52
- dispatch_after ( dispatch_time ( DISPATCH_TIME_NOW, Int64 ( NSEC_PER_SEC) ) , dispatch_get_main_queue ( ) ) {
53
- NSOperationQueue . mainQueue ( ) . addOperation ( operation)
54
- }
54
+ guard reconnectAttemptsMax != nil && reconnectAttempts < reconnectAttemptsMax! else {
55
+ self . debugLogger ? ( " [PUSHER DEBUG] Max reconnect attempts reached " )
56
+ return updateConnectionState ( . Disconnected)
57
+ }
55
58
56
- self . reconnectOperation? . cancel ( )
57
- self . reconnectOperation = operation
58
- }
59
+ guard let reachability = self . reachability where reachability. isReachable ( ) else {
60
+ self . debugLogger ? ( " [PUSHER DEBUG] Network unreachable so waiting to attempt reconnect " )
61
+ return updateConnectionState ( . Disconnected)
62
+ }
63
+
64
+ if connectionState != . Reconnecting {
65
+ updateConnectionState ( . Reconnecting)
66
+ }
67
+ self . debugLogger ? ( " [PUSHER DEBUG] Network reachable so will setup reconnect attempt " )
68
+
69
+ attemptReconnect ( )
70
+ }
71
+
72
+ /**
73
+ Attempt to reconnect triggered by a disconnection
74
+ */
75
+ internal func attemptReconnect( ) {
76
+ if connectionState != . Connected {
77
+ if ( reconnectAttemptsMax == nil || reconnectAttempts < reconnectAttemptsMax!) {
78
+ let reconnectInterval = Double ( reconnectAttempts * reconnectAttempts)
59
79
60
- attemptReconnect ( )
80
+ let timeInterval = maxReconnectGapInSeconds != nil ? min ( reconnectInterval, maxReconnectGapInSeconds!)
81
+ : reconnectInterval
82
+
83
+ self . debugLogger ? ( " [PUSHER DEBUG] Waiting \( timeInterval) seconds before attempting to reconnect (attempt \( reconnectAttempts + 1 ) of \( reconnectAttemptsMax) ) " )
84
+
85
+ reconnectTimer = NSTimer . scheduledTimerWithTimeInterval (
86
+ timeInterval,
87
+ target: self ,
88
+ selector: #selector( connect) ,
89
+ userInfo: nil ,
90
+ repeats: false
91
+ )
92
+ reconnectAttempts += 1
93
+ }
61
94
}
62
95
}
63
96
97
+
64
98
public func websocketDidConnect( ws: WebSocket ) { }
65
99
public func websocketDidReceiveData( ws: WebSocket , data: NSData ) { }
66
100
}
0 commit comments