Skip to content

Commit

Permalink
added header customization.
Browse files Browse the repository at this point in the history
  • Loading branch information
gnap committed Feb 27, 2014
1 parent d3fb9de commit 02be0e1
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
8 changes: 5 additions & 3 deletions SocketIO.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ typedef enum {
NSString *_sid;
NSString *_endpoint;
NSDictionary *_params;
NSDictionary *_headers;

__weak id<SocketIODelegate> _delegate;

Expand Down Expand Up @@ -106,8 +107,9 @@ typedef enum {
- (id) initWithDelegate:(id<SocketIODelegate>)delegate;
- (void) connectToHost:(NSString *)host onPort:(NSInteger)port;
- (void) connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary *)params;
- (void) connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary *)params withNamespace:(NSString *)endpoint;
- (void) connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary *)params withNamespace:(NSString *)endpoint withConnectionTimeout: (NSTimeInterval) connectionTimeout;
- (void) connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary *)params withHeaders: (NSDictionary *) headers;
- (void) connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary *)params withHeaders: (NSDictionary *) headers withNamespace:(NSString *)endpoint;
- (void) connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary *)params withHeaders: (NSDictionary *) headers withNamespace:(NSString *)endpoint withConnectionTimeout: (NSTimeInterval) connectionTimeout;

- (void) disconnect;
- (void) disconnectForced;
Expand All @@ -122,4 +124,4 @@ typedef enum {

- (void) setResourceName:(NSString *)name;

@end
@end
29 changes: 23 additions & 6 deletions SocketIO.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,25 +96,35 @@ - (id) initWithDelegate:(id<SocketIODelegate>)delegate

- (void) connectToHost:(NSString *)host onPort:(NSInteger)port
{
[self connectToHost:host onPort:port withParams:nil withNamespace:@"" withConnectionTimeout:defaultConnectionTimeout];
[self connectToHost:host onPort:port withParams:nil withHeaders: nil withNamespace:@"" withConnectionTimeout:defaultConnectionTimeout];
}

- (void) connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary *)params
{
[self connectToHost:host onPort:port withParams:params withNamespace:@"" withConnectionTimeout:defaultConnectionTimeout];
[self connectToHost:host onPort:port withParams:params withHeaders: nil withNamespace:@"" withConnectionTimeout:defaultConnectionTimeout];
}

- (void) connectToHost:(NSString *)host
onPort:(NSInteger)port
withParams:(NSDictionary *)params
withNamespace:(NSString *)endpoint
withHeaders:(NSDictionary *)headers
{
[self connectToHost:host onPort:port withParams:params withNamespace:endpoint withConnectionTimeout:defaultConnectionTimeout];
[self connectToHost:host onPort:port withParams:params withHeaders: headers withNamespace: @"" withConnectionTimeout:defaultConnectionTimeout];
}

- (void) connectToHost:(NSString *)host
onPort:(NSInteger)port
withParams:(NSDictionary *)params
withHeaders:(NSDictionary *)headers
withNamespace:(NSString *)endpoint
{
[self connectToHost:host onPort:port withParams:params withHeaders: headers withNamespace: endpoint withConnectionTimeout:defaultConnectionTimeout];
}

- (void) connectToHost:(NSString *)host
onPort:(NSInteger)port
withParams:(NSDictionary *)params
withHeaders:(NSDictionary *)headers
withNamespace:(NSString *)endpoint
withConnectionTimeout:(NSTimeInterval)connectionTimeout
{
Expand All @@ -124,6 +134,7 @@ - (void) connectToHost:(NSString *)host
_host = host;
_port = port;
_params = params;
_headers = headers;
_endpoint = [endpoint copy];

// create a query parameters string
Expand All @@ -142,9 +153,15 @@ - (void) connectToHost:(NSString *)host
query = nil;

// make a request
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:handshakeUrl]
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:handshakeUrl]
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:connectionTimeout];

if (headers != nil) {
NSMutableDictionary *newHeaders = [[request allHTTPHeaderFields] mutableCopy];
[newHeaders addEntriesFromDictionary: headers];
[request setAllHTTPHeaderFields: newHeaders];
}

_handshake = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:NO];
[_handshake scheduleInRunLoop:[NSRunLoop mainRunLoop] forMode:NSDefaultRunLoopMode];
Expand Down Expand Up @@ -718,7 +735,7 @@ - (void) connectionDidFinishLoading:(NSURLConnection *)connection
NSString *regex = @"[^0-9]";
NSPredicate *regexTest = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
if ([_sid rangeOfString:@"error"].location != NSNotFound || [regexTest evaluateWithObject:_sid]) {
[self connectToHost:_host onPort:_port withParams:_params withNamespace:_endpoint];
[self connectToHost:_host onPort:_port withParams:_params withHeaders: _headers withNamespace:_endpoint];
return;
}

Expand Down
5 changes: 4 additions & 1 deletion SocketTesterARC/ViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ - (void) viewDidLoad
// socketIO.useSecure = YES;

// connect to the socket.io server that is running locally at port 3000
[socketIO connectToHost:@"localhost" onPort:3000];
NSMutableDictionary *params = [NSMutableDictionary new];
[params setObject: @"6f7a7792814a43ec8f344ed053f1280b" forKey: @"access_token"];
NSDictionary *headers = [NSDictionary dictionaryWithObjectsAndKeys: @"qc_0=123", @"Cookie", nil];
[socketIO connectToHost:@"localhost" onPort:8000 withParams: params withHeaders: headers];
}

# pragma mark -
Expand Down

0 comments on commit 02be0e1

Please sign in to comment.