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

Commit 9f5f39e

Browse files
committed
Merge pull request #86 from mennopruijssers/master
fix #65 and #83
2 parents ce286f9 + 09a4711 commit 9f5f39e

File tree

1 file changed

+35
-1
lines changed

1 file changed

+35
-1
lines changed

SocketIOTransportXHR.m

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,47 @@ - (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)er
202202
}
203203
}
204204

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+
205236
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
206237
{
207238
NSString *message = [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding];
208239
DEBUGLOG(@"response: __%@__", message);
209240

210241
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+
}];
212246
}
213247

214248
// remove current connection from pool

0 commit comments

Comments
 (0)