|
|
@ -20,13 +20,16 @@
|
|
|
|
#import "IncomingPushMessageSignal.pb.h"
|
|
|
|
#import "IncomingPushMessageSignal.pb.h"
|
|
|
|
|
|
|
|
|
|
|
|
#define kWebSocketHeartBeat 15
|
|
|
|
#define kWebSocketHeartBeat 15
|
|
|
|
|
|
|
|
#define kWebSocketReconnectTry 5
|
|
|
|
|
|
|
|
|
|
|
|
NSString * const SocketOpenedNotification = @"SocketOpenedNotification";
|
|
|
|
NSString * const SocketOpenedNotification = @"SocketOpenedNotification";
|
|
|
|
NSString * const SocketClosedNotification = @"SocketClosedNotification";
|
|
|
|
NSString * const SocketClosedNotification = @"SocketClosedNotification";
|
|
|
|
NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
|
|
|
|
NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
|
|
|
|
|
|
|
|
|
|
|
|
@interface TSSocketManager ()
|
|
|
|
@interface TSSocketManager ()
|
|
|
|
@property (nonatomic, retain) NSTimer *timer;
|
|
|
|
@property (nonatomic, retain) NSTimer *pingTimer;
|
|
|
|
|
|
|
|
@property (nonatomic, retain) NSTimer *reconnectTimer;
|
|
|
|
|
|
|
|
|
|
|
|
@property (nonatomic, retain) SRWebSocket *websocket;
|
|
|
|
@property (nonatomic, retain) SRWebSocket *websocket;
|
|
|
|
@property (nonatomic) NSUInteger status;
|
|
|
|
@property (nonatomic) NSUInteger status;
|
|
|
|
@end
|
|
|
|
@end
|
|
|
@ -94,14 +97,18 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
|
|
|
|
#pragma mark - Delegate methods
|
|
|
|
#pragma mark - Delegate methods
|
|
|
|
|
|
|
|
|
|
|
|
- (void) webSocketDidOpen:(SRWebSocket *)webSocket {
|
|
|
|
- (void) webSocketDidOpen:(SRWebSocket *)webSocket {
|
|
|
|
self.timer = [NSTimer scheduledTimerWithTimeInterval:kWebSocketHeartBeat target:self selector:@selector(webSocketHeartBeat) userInfo:nil repeats:YES];
|
|
|
|
self.pingTimer = [NSTimer scheduledTimerWithTimeInterval:kWebSocketHeartBeat target:self selector:@selector(webSocketHeartBeat) userInfo:nil repeats:YES];
|
|
|
|
self.status = kSocketStatusOpen;
|
|
|
|
self.status = kSocketStatusOpen;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
[self.reconnectTimer invalidate];
|
|
|
|
|
|
|
|
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.timer invalidate];
|
|
|
|
[self.pingTimer invalidate];
|
|
|
|
self.status = kSocketStatusClosed;
|
|
|
|
self.status = kSocketStatusClosed;
|
|
|
|
|
|
|
|
[self scheduleRetry];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void) webSocket:(SRWebSocket *)webSocket didReceiveMessage:(NSData*)data {
|
|
|
|
- (void) webSocket:(SRWebSocket *)webSocket didReceiveMessage:(NSData*)data {
|
|
|
@ -161,8 +168,9 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
|
|
|
|
|
|
|
|
|
|
|
|
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean {
|
|
|
|
- (void)webSocket:(SRWebSocket *)webSocket didCloseWithCode:(NSInteger)code reason:(NSString *)reason wasClean:(BOOL)wasClean {
|
|
|
|
DDLogVerbose(@"WebSocket did close");
|
|
|
|
DDLogVerbose(@"WebSocket did close");
|
|
|
|
[self.timer invalidate];
|
|
|
|
[self.pingTimer invalidate];
|
|
|
|
self.status = kSocketStatusClosed;
|
|
|
|
self.status = kSocketStatusClosed;
|
|
|
|
|
|
|
|
[self scheduleRetry];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (void)webSocketHeartBeat {
|
|
|
|
- (void)webSocketHeartBeat {
|
|
|
@ -173,6 +181,12 @@ NSString * const SocketConnectingNotification = @"SocketConnectingNotification";
|
|
|
|
return [NSString stringWithFormat:@"?login=%@&password=%@", [[TSAccountManager registeredNumber] stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"],[TSStorageManager serverAuthToken]];
|
|
|
|
return [NSString stringWithFormat:@"?login=%@&password=%@", [[TSAccountManager registeredNumber] stringByReplacingOccurrencesOfString:@"+" withString:@"%2B"],[TSStorageManager serverAuthToken]];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- (void)scheduleRetry{
|
|
|
|
|
|
|
|
if (!self.reconnectTimer || ![self.reconnectTimer isValid]) {
|
|
|
|
|
|
|
|
self.reconnectTimer = [NSTimer scheduledTimerWithTimeInterval:kWebSocketReconnectTry target:[self class] selector:@selector(becomeActive) userInfo:nil repeats:YES];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#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 {
|
|
|
|