Skip to content
This repository has been archived by the owner on May 14, 2024. It is now read-only.

Commit

Permalink
removed NSURLConnection on main thread
Browse files Browse the repository at this point in the history
started adding xhr support (just the beginning)
  • Loading branch information
pkyeck committed Jun 6, 2012
1 parent 2f2a9be commit f40d224
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 29 deletions.
6 changes: 3 additions & 3 deletions SocketIO.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// SocketIO.h
// v.02 ARC
// v0.21 ARC
//
// based on
// socketio-cocoa https://github.com/fpotter/socketio-cocoa
Expand Down Expand Up @@ -47,7 +47,7 @@ typedef void(^SocketIOCallback)(id argsData);
NSString *_sid;
NSString *_endpoint;

id<SocketIODelegate> _delegate;
__unsafe_unretained id<SocketIODelegate> _delegate;

WebSocket *_webSocket;

Expand All @@ -64,7 +64,7 @@ typedef void(^SocketIOCallback)(id argsData);
NSMutableDictionary *_acks;
NSInteger _ackCount;

//http request
// http request
NSMutableData * _httpRequestData;
}

Expand Down
68 changes: 42 additions & 26 deletions SocketIO.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//
// SocketIO.m
// v.02 ARC
// v0.21 ARC
//
// based on
// socketio-cocoa https://github.com/fpotter/socketio-cocoa
Expand Down Expand Up @@ -28,6 +28,7 @@
#define DEBUG_LOGS 1
#define HANDSHAKE_URL @"http://%@:%d/socket.io/1/?t=%d%@"
#define SOCKET_URL @"ws://%@:%d/socket.io/1/websocket/%@"
#define XHR_URL @"http://%@:%d/socket.io/1/xhr-polling/%@"


# pragma mark -
Expand Down Expand Up @@ -69,8 +70,7 @@ - (id) initWithDelegate:(id<SocketIODelegate>)delegate
_delegate = delegate;
_queue = [[NSMutableArray alloc] init];
_ackCount = 0;
_acks = [[NSMutableDictionary alloc] init];
_httpRequestData = [NSMutableData data];
_acks = [[NSMutableDictionary alloc] init];
}
return self;
}
Expand All @@ -85,15 +85,6 @@ - (void) connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDic
[self connectToHost:host onPort:port withParams:params withNamespace:@""];
}

//convenient methode to call connection request on the main thread
- (void) httpRequestWithRequest:(NSURLRequest*)request {
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request delegate:self];

if(!connection) {
[self connection:connection didFailWithError:nil];
}
}

- (void) connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDictionary *)params withNamespace:(NSString *)endpoint
{
if (!_isConnected && !_isConnecting)
Expand All @@ -117,13 +108,20 @@ - (void) connectToHost:(NSString *)host onPort:(NSInteger)port withParams:(NSDic
query = nil;


//make a request
NSURLRequest *request = [NSURLRequest requestWithURL:url];
// make a request
NSURLRequest *request = [NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:10.0];

//NSURLConnection need to be called on the main thread
[self performSelectorOnMainThread:@selector(httpRequestWithRequest:)
withObject:request
waitUntilDone:YES];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:request
delegate:self];
if (connection) {
_httpRequestData = [NSMutableData data];
}
else {
// connection failed
[self connection:connection didFailWithError:nil];
}
}
}

Expand Down Expand Up @@ -166,7 +164,9 @@ - (void) sendEvent:(NSString *)eventName withData:(NSDictionary *)data
- (void) sendEvent:(NSString *)eventName withData:(NSDictionary *)data andAcknowledge:(SocketIOCallback)function
{
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObject:eventName forKey:@"name"];
if (data != nil) { // do not require arguments

// do not require arguments
if (data != nil) {
[dict setObject:data forKey:@"args"];
}

Expand All @@ -179,7 +179,7 @@ - (void) sendEvent:(NSString *)eventName withData:(NSDictionary *)data andAcknow
[self send:packet];
}

- (void)sendAcknowledgement:(NSString *)pId withArgs:(NSArray *)data
- (void) sendAcknowledgement:(NSString *)pId withArgs:(NSArray *)data
{
SocketIOPacket *packet = [[SocketIOPacket alloc] initWithType:@"ack"];
packet.data = [data JSONRepresentation];
Expand All @@ -200,8 +200,15 @@ - (void) openSocket

_webSocket = [[WebSocket alloc] initWithURLString:url delegate:self];
[self log:[NSString stringWithFormat:@"Opening %@", url]];
[_webSocket open];
[_webSocket open];
}

- (void) openXHRPolling
{
NSString *url = [NSString stringWithFormat:XHR_URL, _host, _port, _sid];
[self log:[NSString stringWithFormat:@"Opening XHR @ %@", url]];

// TODO: implement
}

- (void) sendDisconnect
Expand Down Expand Up @@ -548,15 +555,18 @@ - (NSMutableArray*) getMatchesFrom:(NSString*)data with:(NSString*)regex

# pragma mark -
# pragma mark Handshake callbacks (NSURLConnectionDataDelegate)
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
[_httpRequestData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
- (void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_httpRequestData appendData:data];
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"ERROR: handshake failed ... %@", [error localizedDescription]);

_isConnected = NO;
Expand All @@ -567,7 +577,8 @@ - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)err
}
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
NSString *responseString = [[NSString alloc] initWithData:_httpRequestData encoding:NSASCIIStringEncoding];

[self log:[NSString stringWithFormat:@"requestFinished() %@", responseString]];
Expand All @@ -586,14 +597,19 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSArray *transports = [t componentsSeparatedByString:@","];
[self log:[NSString stringWithFormat:@"transports: %@", transports]];

// TODO: check which transports are supported by the server

// if websocket
[self openSocket];

// TODO: if xhr ...
}


# pragma mark -
# pragma mark WebSocket Delegate Methods

- (void) webSocketDidClose:(WebSocket*)webSocket
- (void) webSocketDidClose:(WebSocket *)webSocket
{
[self log:[NSString stringWithFormat:@"Connection closed."]];
[self onDisconnect];
Expand Down

0 comments on commit f40d224

Please sign in to comment.