@@ -202,13 +202,47 @@ - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)er
202
202
}
203
203
}
204
204
205
+ // Sometimes Socket.IO "batches" up messages in one packet,
206
+ // so we have to split them.
207
+ //
208
+ - (NSArray *)packetsFromPayload : (NSString *)payload
209
+ {
210
+ // "Batched" format is:
211
+ // �[packet_0 length]�[packet_0]�[packet_1 length]�[packet_1]�[packet_n length]�[packet_n]
212
+
213
+ if ([payload hasPrefix: @" \ufffd " ]) {
214
+ // Payload has multiple packets, split based on the '�' character
215
+ // Skip the first character, then split
216
+ NSArray *split = [[payload substringFromIndex: 1 ] componentsSeparatedByString: @" \ufffd " ];
217
+
218
+ // Init array with [split count] / 2 because we only need the odd-numbered
219
+ NSMutableArray *packets = [NSMutableArray arrayWithCapacity: [split count ]/2 ];
220
+
221
+ // Now all of the odd-numbered indices are the packets (1, 3, 5, etc.)
222
+ [split enumerateObjectsUsingBlock: ^(id obj, NSUInteger idx, BOOL *stop) {
223
+ if (idx % 2 != 0 ) {
224
+ [packets addObject: obj];
225
+ }
226
+ }];
227
+
228
+ NSLog (@" Parsed a payload!" );
229
+ return packets;
230
+ } else {
231
+ // Regular single-packet payload
232
+ return @[payload];
233
+ }
234
+ }
235
+
205
236
- (void ) connectionDidFinishLoading : (NSURLConnection *)connection
206
237
{
207
238
NSString *message = [[NSString alloc ] initWithData: _data encoding: NSUTF8StringEncoding];
208
239
DEBUGLOG (@" response: __%@ __" , message);
209
240
210
241
if (![message isEqualToString: @" 1" ]) {
211
- [delegate onData: message];
242
+ NSArray *messages = [self packetsFromPayload: message];
243
+ [messages enumerateObjectsUsingBlock: ^(NSString *message, NSUInteger idx, BOOL *stop) {
244
+ [delegate onData: message];
245
+ }];
212
246
}
213
247
214
248
// remove current connection from pool
0 commit comments