@@ -171,6 +171,27 @@ - (void) poll:(NSString *)data retryNumber:(int)retry
171171
172172- (void ) connection : (NSURLConnection *)connection didReceiveResponse : (NSURLResponse *)response
173173{
174+ // check for server status code (http://gigliwood.com/weblog/Cocoa/Q__When_is_an_conne.html)
175+ if ([response respondsToSelector: @selector (statusCode )]) {
176+ NSInteger statusCode = [((NSHTTPURLResponse *)response) statusCode ];
177+ DEBUGLOG (@" didReceiveResponse() %i " , statusCode);
178+
179+ if (statusCode >= 400 ) {
180+ // stop connecting; no more delegate messages
181+ [connection cancel ];
182+
183+ NSString *error = [NSString stringWithFormat: NSLocalizedString(@" Server returned status code %d " , @" " ), statusCode];
184+ NSDictionary *errorInfo = [NSDictionary dictionaryWithObject: error forKey: NSLocalizedDescriptionKey ];
185+ NSError *statusError = [NSError errorWithDomain: SocketIOError
186+ code: statusCode
187+ userInfo: errorInfo];
188+
189+ if ([delegate respondsToSelector: @selector (onError: )]) {
190+ [delegate onError: statusError];
191+ }
192+ }
193+ }
194+
174195 [_data setLength: 0 ];
175196}
176197
@@ -206,15 +227,13 @@ - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)er
206227 }
207228}
208229
209- // Sometimes Socket.IO "batches" up messages in one packet,
210- // so we have to split them.
211- //
230+ // Sometimes Socket.IO "batches" up messages in one packet, so we have to split them.
212231- (NSArray *)packetsFromPayload : (NSString *)payload
213232{
214233 // "Batched" format is:
215234 // �[packet_0 length]�[packet_0]�[packet_1 length]�[packet_1]�[packet_n length]�[packet_n]
216235
217- if ([payload hasPrefix: @" \ufffd " ]) {
236+ if ([payload hasPrefix: @" \ufffd " ]) {
218237 // Payload has multiple packets, split based on the '�' character
219238 // Skip the first character, then split
220239 NSArray *split = [[payload substringFromIndex: 1 ] componentsSeparatedByString: @" \ufffd " ];
@@ -224,14 +243,15 @@ - (NSArray *)packetsFromPayload:(NSString *)payload
224243
225244 // Now all of the odd-numbered indices are the packets (1, 3, 5, etc.)
226245 [split enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {
227- if (idx % 2 != 0 ) {
246+ if (idx % 2 != 0 ) {
228247 [packets addObject: obj];
229248 }
230249 }];
231250
232251 NSLog (@" Parsed a payload!" );
233252 return packets;
234- } else {
253+ }
254+ else {
235255 // Regular single-packet payload
236256 return @[payload];
237257 }
0 commit comments