|
|
@ -22,6 +22,7 @@
|
|
|
|
#define kWebSocketHeartBeat 30
|
|
|
|
#define kWebSocketHeartBeat 30
|
|
|
|
#define kWebSocketReconnectTry 5
|
|
|
|
#define kWebSocketReconnectTry 5
|
|
|
|
#define kBackgroundConnectTimer 120
|
|
|
|
#define kBackgroundConnectTimer 120
|
|
|
|
|
|
|
|
#define kBackgroundConnectKeepAlive 20
|
|
|
|
|
|
|
|
|
|
|
|
NSString * const SocketOpenedNotification = @"SocketOpenedNotification";
|
|
|
|
NSString * const SocketOpenedNotification = @"SocketOpenedNotification";
|
|
|
|
NSString * const SocketClosedNotification = @"SocketClosedNotification";
|
|
|
|
NSString * const SocketClosedNotification = @"SocketClosedNotification";
|
|
|
@ -30,11 +31,11 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
|
|
|
|
@interface TSSocketManager ()
|
|
|
|
@interface TSSocketManager ()
|
|
|
|
@property (nonatomic, retain) NSTimer *pingTimer;
|
|
|
|
@property (nonatomic, retain) NSTimer *pingTimer;
|
|
|
|
@property (nonatomic, retain) NSTimer *reconnectTimer;
|
|
|
|
@property (nonatomic, retain) NSTimer *reconnectTimer;
|
|
|
|
|
|
|
|
@property (nonatomic, retain) NSTimer *backgroundKeepAliveTimer;
|
|
|
|
|
|
|
|
|
|
|
|
@property (nonatomic, retain) SRWebSocket *websocket;
|
|
|
|
@property (nonatomic, retain) SRWebSocket *websocket;
|
|
|
|
@property (nonatomic) SocketStatus status;
|
|
|
|
@property (nonatomic) SocketStatus status;
|
|
|
|
|
|
|
|
|
|
|
|
@property (nonatomic, retain) NSTimer *backgroundConnectTimer;
|
|
|
|
|
|
|
|
@property (nonatomic) UIBackgroundTaskIdentifier fetchingTaskIdentifier;
|
|
|
|
@property (nonatomic) UIBackgroundTaskIdentifier fetchingTaskIdentifier;
|
|
|
|
|
|
|
|
|
|
|
|
@property BOOL didFetchInBackground;
|
|
|
|
@property BOOL didFetchInBackground;
|
|
|
@ -59,6 +60,8 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
static dispatch_once_t onceToken;
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
dispatch_once(&onceToken, ^{
|
|
|
|
sharedMyManager = [[self alloc] init];
|
|
|
|
sharedMyManager = [[self alloc] init];
|
|
|
|
|
|
|
|
sharedMyManager.fetchingTaskIdentifier = UIBackgroundTaskInvalid;
|
|
|
|
|
|
|
|
sharedMyManager.didFetchInBackground = FALSE;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return sharedMyManager;
|
|
|
|
return sharedMyManager;
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -112,26 +115,31 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark - Delegate methods
|
|
|
|
#pragma mark - Delegate methods
|
|
|
|
|
|
|
|
|
|
|
|
- (void) webSocketDidOpen:(SRWebSocket *)webSocket {
|
|
|
|
- (void)webSocketDidOpen:(SRWebSocket *)webSocket {
|
|
|
|
self.pingTimer = [NSTimer scheduledTimerWithTimeInterval:kWebSocketHeartBeat
|
|
|
|
self.pingTimer = [NSTimer scheduledTimerWithTimeInterval:kWebSocketHeartBeat
|
|
|
|
target:self
|
|
|
|
target:self
|
|
|
|
selector:@selector(webSocketHeartBeat)
|
|
|
|
selector:@selector(webSocketHeartBeat)
|
|
|
|
userInfo:nil
|
|
|
|
userInfo:nil
|
|
|
|
repeats:YES];
|
|
|
|
repeats:YES];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Additionally, we want the ping timer to work in the background too.
|
|
|
|
|
|
|
|
[[NSRunLoop mainRunLoop] addTimer:self.pingTimer
|
|
|
|
|
|
|
|
forMode:NSDefaultRunLoopMode];
|
|
|
|
|
|
|
|
|
|
|
|
self.status = kSocketStatusOpen;
|
|
|
|
self.status = kSocketStatusOpen;
|
|
|
|
|
|
|
|
|
|
|
|
[self.reconnectTimer invalidate];
|
|
|
|
[self.reconnectTimer invalidate];
|
|
|
|
self.reconnectTimer = nil;
|
|
|
|
self.reconnectTimer = nil;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void) webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error {
|
|
|
|
- (void)webSocket:(SRWebSocket *)webSocket didFailWithError:(NSError *)error {
|
|
|
|
DDLogError(@"Error connecting to socket %@", error);
|
|
|
|
DDLogError(@"Error connecting to socket %@", error);
|
|
|
|
[self.pingTimer invalidate];
|
|
|
|
[self.pingTimer invalidate];
|
|
|
|
self.status = kSocketStatusClosed;
|
|
|
|
self.status = kSocketStatusClosed;
|
|
|
|
[self scheduleRetry];
|
|
|
|
[self scheduleRetry];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void) webSocket:(SRWebSocket *)webSocket didReceiveMessage:(NSData*)data {
|
|
|
|
- (void)webSocket:(SRWebSocket *)webSocket didReceiveMessage:(NSData*)data {
|
|
|
|
WebSocketMessage *wsMessage = [WebSocketMessage parseFromData:data];
|
|
|
|
WebSocketMessage *wsMessage = [WebSocketMessage parseFromData:data];
|
|
|
|
self.didFetchInBackground = YES;
|
|
|
|
self.didFetchInBackground = YES;
|
|
|
|
|
|
|
|
|
|
|
@ -149,6 +157,8 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
|
|
|
|
|
|
|
|
|
|
|
|
[self sendWebSocketMessageAcknowledgement:message];
|
|
|
|
[self sendWebSocketMessageAcknowledgement:message];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[self keepAliveBackground];
|
|
|
|
|
|
|
|
|
|
|
|
if ([message.path isEqualToString:@"/api/v1/message"] && [message.verb isEqualToString:@"PUT"]){
|
|
|
|
if ([message.path isEqualToString:@"/api/v1/message"] && [message.verb isEqualToString:@"PUT"]){
|
|
|
|
NSData *decryptedPayload = [Cryptography decryptAppleMessagePayload:message.body
|
|
|
|
NSData *decryptedPayload = [Cryptography decryptAppleMessagePayload:message.body
|
|
|
|
withSignalingKey:TSStorageManager.signalingKey];
|
|
|
|
withSignalingKey:TSStorageManager.signalingKey];
|
|
|
@ -166,6 +176,22 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- (void)keepAliveBackground {
|
|
|
|
|
|
|
|
if (self.fetchingTaskIdentifier) {
|
|
|
|
|
|
|
|
[self.backgroundKeepAliveTimer invalidate];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
self.backgroundKeepAliveTimer = [NSTimer scheduledTimerWithTimeInterval:kBackgroundConnectKeepAlive
|
|
|
|
|
|
|
|
target:self
|
|
|
|
|
|
|
|
selector:@selector(backgroundTimeExpired)
|
|
|
|
|
|
|
|
userInfo:nil
|
|
|
|
|
|
|
|
repeats:NO];
|
|
|
|
|
|
|
|
// Additionally, we want the reconnect timer to work in the background too.
|
|
|
|
|
|
|
|
[[NSRunLoop mainRunLoop] addTimer:self.backgroundKeepAliveTimer
|
|
|
|
|
|
|
|
forMode:NSDefaultRunLoopMode];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)processWebSocketResponseMessage:(WebSocketResponseMessage*)message {
|
|
|
|
- (void)processWebSocketResponseMessage:(WebSocketResponseMessage*)message {
|
|
|
|
DDLogWarn(@"Client should not receive WebSocket Respond messages");
|
|
|
|
DDLogWarn(@"Client should not receive WebSocket Respond messages");
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -215,6 +241,9 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
|
|
|
|
selector:@selector(becomeActive)
|
|
|
|
selector:@selector(becomeActive)
|
|
|
|
userInfo:nil
|
|
|
|
userInfo:nil
|
|
|
|
repeats:YES];
|
|
|
|
repeats:YES];
|
|
|
|
|
|
|
|
// Additionally, we want the reconnect timer to work in the background too.
|
|
|
|
|
|
|
|
[[NSRunLoop mainRunLoop] addTimer:self.reconnectTimer
|
|
|
|
|
|
|
|
forMode:NSDefaultRunLoopMode];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -222,10 +251,11 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
|
|
|
|
|
|
|
|
|
|
|
|
+ (void)becomeActiveFromForeground {
|
|
|
|
+ (void)becomeActiveFromForeground {
|
|
|
|
TSSocketManager *sharedInstance = [self sharedManager];
|
|
|
|
TSSocketManager *sharedInstance = [self sharedManager];
|
|
|
|
|
|
|
|
[sharedInstance.backgroundKeepAliveTimer invalidate];
|
|
|
|
|
|
|
|
|
|
|
|
[sharedInstance.backgroundConnectTimer invalidate];
|
|
|
|
if (sharedInstance.fetchingTaskIdentifier != UIBackgroundTaskInvalid) {
|
|
|
|
sharedInstance.backgroundConnectTimer = nil;
|
|
|
|
[sharedInstance closeBackgroundTask];
|
|
|
|
sharedInstance.fetchingTaskIdentifier = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[self becomeActive];
|
|
|
|
[self becomeActive];
|
|
|
|
}
|
|
|
|
}
|
|
|
@ -234,25 +264,34 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
|
|
|
|
+ (void)becomeActiveFromBackground {
|
|
|
|
+ (void)becomeActiveFromBackground {
|
|
|
|
TSSocketManager *sharedInstance = [TSSocketManager sharedManager];
|
|
|
|
TSSocketManager *sharedInstance = [TSSocketManager sharedManager];
|
|
|
|
|
|
|
|
|
|
|
|
if (sharedInstance.fetchingTaskIdentifier == 0) {
|
|
|
|
if (sharedInstance.fetchingTaskIdentifier == UIBackgroundTaskInvalid) {
|
|
|
|
|
|
|
|
[sharedInstance.backgroundKeepAliveTimer invalidate];
|
|
|
|
sharedInstance.didFetchInBackground = NO;
|
|
|
|
sharedInstance.didFetchInBackground = NO;
|
|
|
|
sharedInstance.fetchingTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
|
|
|
|
sharedInstance.fetchingTaskIdentifier = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
|
|
|
|
if (!sharedInstance.didFetchInBackground) {
|
|
|
|
|
|
|
|
[sharedInstance backgroundConnectTimedOut];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sharedInstance.fetchingTaskIdentifier = 0;
|
|
|
|
|
|
|
|
[TSSocketManager resignActivity];
|
|
|
|
[TSSocketManager resignActivity];
|
|
|
|
|
|
|
|
[sharedInstance closeBackgroundTask];
|
|
|
|
}];
|
|
|
|
}];
|
|
|
|
|
|
|
|
|
|
|
|
[self becomeActive];
|
|
|
|
[self becomeActive];
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
DDLogWarn(@"Got called to become active in the background but there was already a background task running.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- (void)backgroundTimeExpired {
|
|
|
|
|
|
|
|
[[self class] resignActivity];
|
|
|
|
|
|
|
|
[self closeBackgroundTask];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)closeBackgroundTask {
|
|
|
|
- (void)closeBackgroundTask {
|
|
|
|
UIBackgroundTaskIdentifier identifier = self.fetchingTaskIdentifier;
|
|
|
|
UIBackgroundTaskIdentifier identifier = self.fetchingTaskIdentifier;
|
|
|
|
self.fetchingTaskIdentifier = 0;
|
|
|
|
self.fetchingTaskIdentifier = UIBackgroundTaskInvalid;
|
|
|
|
|
|
|
|
[self.backgroundKeepAliveTimer invalidate];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!self.didFetchInBackground) {
|
|
|
|
|
|
|
|
[self backgroundConnectTimedOut];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[TSSocketManager resignActivity];
|
|
|
|
|
|
|
|
[[UIApplication sharedApplication] endBackgroundTask:identifier];
|
|
|
|
[[UIApplication sharedApplication] endBackgroundTask:identifier];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
@ -262,7 +301,6 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
|
|
|
|
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
|
|
|
|
[[UIApplication sharedApplication] presentLocalNotificationNow:notification];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma mark UI Delegates
|
|
|
|
#pragma mark UI Delegates
|
|
|
|
|
|
|
|
|
|
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
|
|
|
|
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
|
|
|
|