Skip to content

Commit

Permalink
Make sure auto-reconnect happens if a connection attempt fails.
Browse files Browse the repository at this point in the history
Should also make sure that the correct PTPusher delegate message is sent when failure occurs.
  • Loading branch information
lukeredpath committed Aug 23, 2011
1 parent 6a8e155 commit 4faa0d5
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Library/PTPusher.h
Expand Up @@ -71,7 +71,8 @@ extern NSString *const PTPusherErrorUnderlyingEventKey;
@property (nonatomic, assign) id<PTPusherDelegate> delegate;


/** Indicates whether the client should attempt to reconnect automatically when disconnected.
/** Indicates whether the client should attempt to reconnect automatically when disconnected
or if the connection failed.
When YES, the client will automatically attempt to re-establish a connection after a set delay.
Expand Down
30 changes: 20 additions & 10 deletions Library/PTPusher.m
Expand Up @@ -35,6 +35,7 @@ @interface PTPusher ()
@property (nonatomic, retain, readwrite) PTPusherConnection *connection;

- (void)subscribeToChannel:(PTPusherChannel *)channel;
- (void)reconnectAfterDelay;
@end

@interface PTPusherChannel ()
Expand Down Expand Up @@ -222,23 +223,18 @@ - (void)pusherConnectionDidDisconnect:(PTPusherConnection *)connection
if ([self.delegate respondsToSelector:@selector(pusher:connectionDidDisconnect:)]) {
[self.delegate pusher:self connectionDidDisconnect:connection];
}

if (self.shouldReconnectAutomatically) {
if ([self.delegate respondsToSelector:@selector(pusher:connectionWillReconnect:afterDelay:)]) {
[self.delegate pusher:self connectionWillReconnect:connection afterDelay:self.reconnectDelay];
}

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, self.reconnectDelay * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[connection connect];
});
[self reconnectAfterDelay];
}
}

- (void)pusherConnection:(PTPusherConnection *)connection didFailWithError:(NSError *)error
{
if ([self.delegate respondsToSelector:@selector(pusher:connectionDidDisconnect:)]) {
[self.delegate pusher:self connectionDidDisconnect:connection];
[self.delegate pusher:self connection:connection failedWithError:error];
}
if ([error.domain isEqualToString:ZTWebSocketErrorDomain] && self.shouldReconnectAutomatically) {
[self reconnectAfterDelay];
}
}

Expand All @@ -255,4 +251,18 @@ - (void)pusherConnection:(PTPusherConnection *)connection didReceiveEvent:(PTPus
userInfo:[NSDictionary dictionaryWithObject:event forKey:PTPusherEventUserInfoKey]];
}

#pragma mark - Private

- (void)reconnectAfterDelay
{
if ([self.delegate respondsToSelector:@selector(pusher:connectionWillReconnect:afterDelay:)]) {
[self.delegate pusher:self connectionWillReconnect:_connection afterDelay:self.reconnectDelay];
}

dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, self.reconnectDelay * NSEC_PER_SEC);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
[_connection connect];
});
}

@end
6 changes: 6 additions & 0 deletions Library/PTPusherDelegate.h
Expand Up @@ -37,6 +37,12 @@

/** Notifies the delegate that the PTPusher instance failed to connect to the Pusher service.
If reconnectAutomatically is YES, PTPusher will attempt to reconnect if the initial connection failed.
This reconnect attempt will happen after this message is sent to the delegate, giving the delegate
a chance to inspect the connection error and disable automatic reconnection if it thinks the reconnection
attempt is likely to fail, depending on the error.
@param pusher The PTPusher instance that has connected.
@param connection The connection for the pusher instance.
@param error The connection error.
Expand Down

0 comments on commit 4faa0d5

Please sign in to comment.