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

Commit

Permalink
v0.5.2
Browse files Browse the repository at this point in the history
-  check HTTP status code of NSURLConnection responses during xhr polling transport. close #160
  • Loading branch information
pkyeck committed Mar 23, 2014
2 parents b28b395 + d12c7a1 commit b4e3f2f
Showing 1 changed file with 26 additions and 6 deletions.
32 changes: 26 additions & 6 deletions SocketIOTransportXHR.m
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,27 @@ - (void) poll:(NSString *)data retryNumber:(int)retry

- (void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
// check for server status code (http://gigliwood.com/weblog/Cocoa/Q__When_is_an_conne.html)
if ([response respondsToSelector:@selector(statusCode)]) {
NSInteger statusCode = [((NSHTTPURLResponse *)response) statusCode];
DEBUGLOG(@"didReceiveResponse() %i", statusCode);

if (statusCode >= 400) {
// stop connecting; no more delegate messages
[connection cancel];

NSString *error = [NSString stringWithFormat:NSLocalizedString(@"Server returned status code %d", @""), statusCode];
NSDictionary *errorInfo = [NSDictionary dictionaryWithObject:error forKey:NSLocalizedDescriptionKey];
NSError *statusError = [NSError errorWithDomain:SocketIOError
code:statusCode
userInfo:errorInfo];

if ([delegate respondsToSelector:@selector(onError:)]) {
[delegate onError:statusError];
}
}
}

[_data setLength:0];
}

Expand Down Expand Up @@ -206,15 +227,13 @@ - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)er
}
}

// Sometimes Socket.IO "batches" up messages in one packet,
// so we have to split them.
//
// Sometimes Socket.IO "batches" up messages in one packet, so we have to split them.
- (NSArray *)packetsFromPayload:(NSString *)payload
{
// "Batched" format is:
// �[packet_0 length]�[packet_0]�[packet_1 length]�[packet_1]�[packet_n length]�[packet_n]

if([payload hasPrefix:@"\ufffd"]) {
if ([payload hasPrefix:@"\ufffd"]) {
// Payload has multiple packets, split based on the '�' character
// Skip the first character, then split
NSArray *split = [[payload substringFromIndex:1] componentsSeparatedByString:@"\ufffd"];
Expand All @@ -224,14 +243,15 @@ - (NSArray *)packetsFromPayload:(NSString *)payload

// Now all of the odd-numbered indices are the packets (1, 3, 5, etc.)
[split enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
if(idx % 2 != 0) {
if (idx % 2 != 0) {
[packets addObject:obj];
}
}];

NSLog(@"Parsed a payload!");
return packets;
} else {
}
else {
// Regular single-packet payload
return @[payload];
}
Expand Down

0 comments on commit b4e3f2f

Please sign in to comment.