@@ -25,6 +25,17 @@ open class PusherConnection: NSObject {
25
25
open weak var delegate : PusherDelegate ?
26
26
internal var reconnectTimer : Timer ? = nil
27
27
28
+ open var socketConnected : Bool = false {
29
+ didSet {
30
+ updateConnectionStateAndAttemptSubscriptions ( )
31
+ }
32
+ }
33
+ open var connectionEstablishedMessageReceived : Bool = false {
34
+ didSet {
35
+ updateConnectionStateAndAttemptSubscriptions ( )
36
+ }
37
+ }
38
+
28
39
open lazy var reachability : Reachability ? = {
29
40
let reachability = Reachability . init ( )
30
41
reachability? . whenReachable = { [ unowned self] reachability in
@@ -262,6 +273,16 @@ open class PusherConnection: NSObject {
262
273
self . delegate? . changedConnectionState ? ( from: oldState, to: newState)
263
274
}
264
275
276
+ /**
277
+ Update connection state and attempt subscriptions to unsubscribed channels
278
+ */
279
+ fileprivate func updateConnectionStateAndAttemptSubscriptions( ) {
280
+ if self . connectionEstablishedMessageReceived && self . socketConnected && self . connectionState != . connected {
281
+ updateConnectionState ( to: . connected)
282
+ attemptSubscriptionsToUnsubscribedChannels ( )
283
+ }
284
+ }
285
+
265
286
/**
266
287
Handle setting channel state and triggering unsent client events, if applicable,
267
288
upon receiving a successful subscription event
@@ -307,17 +328,23 @@ open class PusherConnection: NSObject {
307
328
if let data = json [ " data " ] as? String {
308
329
if let connectionData = getPusherEventJSON ( from: data) , let socketId = connectionData [ " socket_id " ] as? String {
309
330
self . socketId = socketId
310
- updateConnectionState ( to: . connected)
311
-
312
331
self . reconnectAttempts = 0
313
332
self . reconnectTimer? . invalidate ( )
314
333
315
- for (_, channel) in self . channels. channels {
316
- if !channel. subscribed {
317
- if !self . authorize ( channel) {
318
- print ( " Unable to subscribe to channel: \( channel. name) " )
319
- }
320
- }
334
+ self . connectionEstablishedMessageReceived = true
335
+ }
336
+ }
337
+ }
338
+
339
+ /**
340
+ Attempts to make subscriptions that couldn't be attempted while the
341
+ connection was not in a connected state
342
+ */
343
+ fileprivate func attemptSubscriptionsToUnsubscribedChannels( ) {
344
+ for (_, channel) in self . channels. channels {
345
+ if !channel. subscribed {
346
+ if !self . authorize ( channel) {
347
+ print ( " Unable to subscribe to channel: \( channel. name) " )
321
348
}
322
349
}
323
350
}
0 commit comments