@@ -171,6 +171,27 @@ - (void) poll:(NSString *)data retryNumber:(int)retry
171
171
172
172
- (void ) connection : (NSURLConnection *)connection didReceiveResponse : (NSURLResponse *)response
173
173
{
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
+
174
195
[_data setLength: 0 ];
175
196
}
176
197
@@ -206,15 +227,13 @@ - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)er
206
227
}
207
228
}
208
229
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.
212
231
- (NSArray *)packetsFromPayload : (NSString *)payload
213
232
{
214
233
// "Batched" format is:
215
234
// �[packet_0 length]�[packet_0]�[packet_1 length]�[packet_1]�[packet_n length]�[packet_n]
216
235
217
- if ([payload hasPrefix: @" \ufffd " ]) {
236
+ if ([payload hasPrefix: @" \ufffd " ]) {
218
237
// Payload has multiple packets, split based on the '�' character
219
238
// Skip the first character, then split
220
239
NSArray *split = [[payload substringFromIndex: 1 ] componentsSeparatedByString: @" \ufffd " ];
@@ -224,14 +243,15 @@ - (NSArray *)packetsFromPayload:(NSString *)payload
224
243
225
244
// Now all of the odd-numbered indices are the packets (1, 3, 5, etc.)
226
245
[split enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {
227
- if (idx % 2 != 0 ) {
246
+ if (idx % 2 != 0 ) {
228
247
[packets addObject: obj];
229
248
}
230
249
}];
231
250
232
251
NSLog (@" Parsed a payload!" );
233
252
return packets;
234
- } else {
253
+ }
254
+ else {
235
255
// Regular single-packet payload
236
256
return @[payload];
237
257
}
0 commit comments