Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions SignalR.Client/Transports/SRHttpBasedTransport.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,13 @@
//

#import <Foundation/Foundation.h>
#import <AFNetworking/AFNetworking.h>
#import "SRClientTransportInterface.h"

@interface SRHttpBasedTransport : NSObject <SRClientTransportInterface>

@property (nonatomic, strong) AFHTTPRequestOperationManager* requestManager;

- (void)completeAbort;
- (BOOL)tryCompleteAbort;
- (void)processResponse:(id <SRConnectionInterface>)connection response:(NSString *)response shouldReconnect:(BOOL *)shouldReconnect disconnected:(BOOL *)disconnected;
Expand Down
94 changes: 49 additions & 45 deletions SignalR.Client/Transports/SRHttpBasedTransport.m
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -48,30 +57,30 @@ - (BOOL)supportsKeepAlive {
}

- (void)negotiate:(id<SRConnectionInterface>)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];
}

Expand All @@ -87,25 +96,23 @@ - (void)send:(id<SRConnectionInterface>)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];
}

Expand Down Expand Up @@ -146,17 +153,14 @@ - (void)abort:(id<SRConnectionInterface>)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];
}
Expand Down
141 changes: 69 additions & 72 deletions SignalR.Client/Transports/SRLongPollingTransport.m
Original file line number Diff line number Diff line change
Expand Up @@ -108,92 +108,89 @@ - (void)poll:(id<SRConnectionInterface>)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];
}

Expand Down