From d68814b6677305a6ddf2e1fb183286e343355fcd Mon Sep 17 00:00:00 2001 From: hayashi311 Date: Fri, 22 Apr 2016 17:16:31 +0900 Subject: [PATCH] Customize HTTP request via AFHTTPRequestOperationManager --- .../Transports/SRHttpBasedTransport.h | 3 + .../Transports/SRHttpBasedTransport.m | 94 ++++++------ .../Transports/SRLongPollingTransport.m | 141 +++++++++--------- 3 files changed, 121 insertions(+), 117 deletions(-) diff --git a/SignalR.Client/Transports/SRHttpBasedTransport.h b/SignalR.Client/Transports/SRHttpBasedTransport.h index d58b07f0..9e180d7c 100644 --- a/SignalR.Client/Transports/SRHttpBasedTransport.h +++ b/SignalR.Client/Transports/SRHttpBasedTransport.h @@ -21,10 +21,13 @@ // #import +#import #import "SRClientTransportInterface.h" @interface SRHttpBasedTransport : NSObject +@property (nonatomic, strong) AFHTTPRequestOperationManager* requestManager; + - (void)completeAbort; - (BOOL)tryCompleteAbort; - (void)processResponse:(id )connection response:(NSString *)response shouldReconnect:(BOOL *)shouldReconnect disconnected:(BOOL *)disconnected; diff --git a/SignalR.Client/Transports/SRHttpBasedTransport.m b/SignalR.Client/Transports/SRHttpBasedTransport.m index 6b71a6e3..9bb5ea8d 100644 --- a/SignalR.Client/Transports/SRHttpBasedTransport.m +++ b/SignalR.Client/Transports/SRHttpBasedTransport.m @@ -36,6 +36,15 @@ @interface SRHttpBasedTransport() @implementation SRHttpBasedTransport +- (instancetype)init { + self = [super init]; + if (self) { + self.requestManager = [AFHTTPRequestOperationManager manager]; + self.requestManager.responseSerializer = [AFJSONResponseSerializer serializer]; + } + return self; +} + #pragma mark #pragma mark SRClientTransportInterface @@ -48,30 +57,30 @@ - (BOOL)supportsKeepAlive { } - (void)negotiate:(id)connection connectionData:(NSString *)connectionData completionHandler:(void (^)(SRNegotiationResponse * response, NSError *error))block { - + + id parameters = [self connectionParameters:connection connectionData:connectionData]; NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:[connection.url stringByAppendingString:@"negotiate"] parameters:parameters error:nil]; + + SRLogTransportDebug(@"will negotiate at url: %@", [[request URL] absoluteString]); + [connection prepareRequest:request]; //TODO: prepareRequest [request setTimeoutInterval:30]; - - AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; - [operation setResponseSerializer:[AFJSONResponseSerializer serializer]]; - //operation.shouldUseCredentialStorage = self.shouldUseCredentialStorage; - //operation.credential = self.credential; - //operation.securityPolicy = self.securityPolicy; - SRLogTransportDebug(@"will negotiate at url: %@", [[request URL] absoluteString]); - [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { - SRLogTransportInfo(@"negotiate was successful %@", responseObject); - if(block) { - block([[SRNegotiationResponse alloc] initWithDictionary:responseObject], nil); - } - } failure:^(AFHTTPRequestOperation *operation, NSError *error) { - SRLogTransportError(@"negotiate failed %@", error); - if(block) { - block(nil, error); - } + + AFHTTPRequestOperation *operation = [self.requestManager HTTPRequestOperationWithRequest:request + success:^(AFHTTPRequestOperation* operation, id responseObject) { + SRLogTransportInfo(@"negotiate was successful %@", responseObject); + if(block) { + block([[SRNegotiationResponse alloc] initWithDictionary:responseObject], nil); + } + } failure:^(AFHTTPRequestOperation* operation, NSError* error) { + SRLogTransportError(@"negotiate failed %@", error); + if(block) { + block(nil, error); + } }]; + [operation start]; } @@ -87,25 +96,23 @@ - (void)send:(id)connection data:(NSString *)data connect NSMutableURLRequest *url = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:[connection.url stringByAppendingString:@"send"] parameters:parameters error:nil]; NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:[[url URL] absoluteString] parameters:@{ @"data" : data } error:nil]; [connection prepareRequest:request]; //TODO: prepareRequest - AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; - [operation setResponseSerializer:[AFJSONResponseSerializer serializer]]; - //operation.shouldUseCredentialStorage = self.shouldUseCredentialStorage; - //operation.credential = self.credential; - //operation.securityPolicy = self.securityPolicy; + SRLogTransportDebug(@"will send at url: %@", [[request URL] absoluteString]); - [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { - SRLogTransportInfo(@"send was successful %@", responseObject); - [connection didReceiveData:responseObject]; - if(block) { - block(responseObject, nil); - } - } failure:^(AFHTTPRequestOperation *operation, NSError *error) { - SRLogTransportError(@"send failed %@", error); - [connection didReceiveError:error]; - if (block) { - block(nil, error); - } + AFHTTPRequestOperation *operation = [self.requestManager HTTPRequestOperationWithRequest:request + success:^(AFHTTPRequestOperation* operation, id responseObject) { + SRLogTransportInfo(@"send was successful %@", responseObject); + [connection didReceiveData:responseObject]; + if(block) { + block(responseObject, nil); + } + } failure:^(AFHTTPRequestOperation* operation, NSError* error) { + SRLogTransportError(@"send failed %@", error); + [connection didReceiveError:error]; + if (block) { + block(nil, error); + } }]; + [operation start]; } @@ -146,17 +153,14 @@ - (void)abort:(id)connection timeout:(NSNumber *)timeout NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"POST" URLString:[[url URL] absoluteString] parameters:nil error:nil]; [connection prepareRequest:request]; //TODO: prepareRequest [request setTimeoutInterval:2]; - AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; - [operation setResponseSerializer:[AFJSONResponseSerializer serializer]]; - //operation.shouldUseCredentialStorage = self.shouldUseCredentialStorage; - //operation.credential = self.credential; - //operation.securityPolicy = self.securityPolicy; + SRLogTransportDebug(@"will abort at url: %@", [[request URL] absoluteString]); - [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { - SRLogTransportInfo(@"abort was successful %@", responseObject); - } failure:^(AFHTTPRequestOperation *operation, NSError *error) { - SRLogTransportError(@"abort failed %@",error); - [self completeAbort]; + AFHTTPRequestOperation *operation = [self.requestManager HTTPRequestOperationWithRequest:request + success:^(AFHTTPRequestOperation* operation, id responseObject) { + SRLogTransportInfo(@"abort was successful %@", responseObject); + } failure:^(AFHTTPRequestOperation* operation, NSError* error) { + SRLogTransportError(@"abort failed %@",error); + [self completeAbort]; }]; [operation start]; } diff --git a/SignalR.Client/Transports/SRLongPollingTransport.m b/SignalR.Client/Transports/SRLongPollingTransport.m index 11c7b48c..2d9763c5 100644 --- a/SignalR.Client/Transports/SRLongPollingTransport.m +++ b/SignalR.Client/Transports/SRLongPollingTransport.m @@ -108,92 +108,89 @@ - (void)poll:(id)connection connectionData:(NSString *)co @"groupsToken" : ([connection groupsToken]) ? [connection groupsToken] : @"", @"connectionData" : (connectionData) ? connectionData : @"", }; - + if ([connection queryString]) { NSMutableDictionary *_parameters = [NSMutableDictionary dictionaryWithDictionary:parameters]; [_parameters addEntriesFromDictionary:[connection queryString]]; parameters = _parameters; } - + NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] requestWithMethod:@"GET" URLString:url parameters:parameters error:nil]; [connection prepareRequest:request]; //TODO: prepareRequest [request setTimeoutInterval:240]; - + SRLogLPDebug(@"longPolling will connect at url: %@", [[request URL] absoluteString]); - AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; - [operation setResponseSerializer:[AFJSONResponseSerializer serializer]]; - //operation.shouldUseCredentialStorage = self.shouldUseCredentialStorage; - //operation.credential = self.credential; - //operation.securityPolicy = self.securityPolicy; - [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { - __strong __typeof(&*weakSelf)strongSelf = weakSelf; - __strong __typeof(&*weakConnection)strongConnection = weakConnection; - - BOOL shouldReconnect = NO; - BOOL disconnectedReceived = NO; - - SRLogLPInfo(@"longPolling did receive: %@", operation.responseString); - - [strongSelf processResponse:strongConnection response:operation.responseString shouldReconnect:&shouldReconnect disconnected:&disconnectedReceived]; - if (block) { - block(nil, nil); - } - - if ([strongSelf isConnectionReconnecting:strongConnection]) { - // If the timeout for the reconnect hasn't fired as yet just fire the - // event here before any incoming messages are processed - SRLogLPWarn(@"reconnecting"); - [strongSelf connectionReconnect:strongConnection canReconnect:canReconnect]; - } - - if (shouldReconnect) { + AFHTTPRequestOperation* operation = [self.requestManager HTTPRequestOperationWithRequest:request + success:^(AFHTTPRequestOperation* operation, id responseObject) { + __strong __typeof(&*weakSelf)strongSelf = weakSelf; + __strong __typeof(&*weakConnection)strongConnection = weakConnection; + + BOOL shouldReconnect = NO; + BOOL disconnectedReceived = NO; + + SRLogLPInfo(@"longPolling did receive: %@", operation.responseString); + + [strongSelf processResponse:strongConnection response:operation.responseString shouldReconnect:&shouldReconnect disconnected:&disconnectedReceived]; + if (block) { + block(nil, nil); + } + + if ([strongSelf isConnectionReconnecting:strongConnection]) { + // If the timeout for the reconnect hasn't fired as yet just fire the + // event here before any incoming messages are processed + SRLogLPWarn(@"reconnecting"); + [strongSelf connectionReconnect:strongConnection canReconnect:canReconnect]; + } + + if (shouldReconnect) { + // Transition into reconnecting state + SRLogLPDebug(@"longPolling did receive shouldReconnect command from server"); + [SRConnection ensureReconnecting:strongConnection]; + } + + if (disconnectedReceived) { + SRLogLPDebug(@"longPolling did receive disconnect command from server"); + [strongConnection disconnect]; + } + + if (![strongSelf tryCompleteAbort]) { + //Abort has not been called so continue polling... + canReconnect = @(YES); + [strongSelf poll:strongConnection connectionData:connectionData completionHandler:nil]; + } else { + SRLogLPWarn(@"longPolling has shutdown due to abort"); + } + } failure:^(AFHTTPRequestOperation* operation, NSError* error) { + __strong __typeof(&*weakSelf)strongSelf = weakSelf; + __strong __typeof(&*weakConnection)strongConnection = weakConnection; + + SRLogLPError(@"longPolling did fail with error %@", error); + + canReconnect = @(NO); + // Transition into reconnecting state - SRLogLPDebug(@"longPolling did receive shouldReconnect command from server"); [SRConnection ensureReconnecting:strongConnection]; - } - - if (disconnectedReceived) { - SRLogLPDebug(@"longPolling did receive disconnect command from server"); - [strongConnection disconnect]; - } - - if (![strongSelf tryCompleteAbort]) { - //Abort has not been called so continue polling... - canReconnect = @(YES); - [strongSelf poll:strongConnection connectionData:connectionData completionHandler:nil]; - } else { - SRLogLPWarn(@"longPolling has shutdown due to abort"); - } - } failure:^(AFHTTPRequestOperation *operation, NSError *error) { - __strong __typeof(&*weakSelf)strongSelf = weakSelf; - __strong __typeof(&*weakConnection)strongConnection = weakConnection; - SRLogLPError(@"longPolling did fail with error %@", error); - - canReconnect = @(NO); - - // Transition into reconnecting state - [SRConnection ensureReconnecting:strongConnection]; - - if (![strongSelf tryCompleteAbort] && - ![SRExceptionHelper isRequestAborted:error]) { - [strongConnection didReceiveError:error]; - - SRLogLPDebug(@"will poll again in %ld seconds",(long)[_errorDelay integerValue]); - - canReconnect = @(YES); - - [[NSBlockOperation blockOperationWithBlock:^{ - [strongSelf poll:strongConnection connectionData:connectionData completionHandler:nil]; - }] performSelector:@selector(start) withObject:nil afterDelay:[strongSelf.errorDelay integerValue]]; - - } else { - [strongSelf completeAbort]; - if (block) { - block(nil,error); + if (![strongSelf tryCompleteAbort] && + ![SRExceptionHelper isRequestAborted:error]) { + [strongConnection didReceiveError:error]; + + SRLogLPDebug(@"will poll again in %ld seconds",(long)[_errorDelay integerValue]); + + canReconnect = @(YES); + + [[NSBlockOperation blockOperationWithBlock:^{ + [strongSelf poll:strongConnection connectionData:connectionData completionHandler:nil]; + }] performSelector:@selector(start) withObject:nil afterDelay:[strongSelf.errorDelay integerValue]]; + + } else { + [strongSelf completeAbort]; + if (block) { + block(nil,error); + } } - } }]; + [self.pollingOperationQueue addOperation:operation]; }