Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
- collectedData = [NSMutableData data];
- switch (streamEvent) {
- case NSStreamEventOpenCompleted:
- NSLog(@"Stream opened");
- break;
- case NSStreamEventHasBytesAvailable:
- if (theStream == inputStream) {
- NSData *nl = [@"n" dataUsingEncoding:NSUTF8StringEncoding];
- uint8_t bufferz[1024];
- int lenz;
- while ([inputStream hasBytesAvailable]) {
- lenz = [inputStream read:bufferz maxLength:sizeof(bufferz)];
- [collectedData appendBytes: (const void *)bufferz length:lenz];
- }
- NSRange nlRange =[collectedData rangeOfData:nl options:0 range:NSMakeRange(0, [collectedData length])];
- while (nlRange.location != NSNotFound) {
- // Extract data from the beginning up to (but not including) the newline character:
- NSData *jsonData = [collectedData subdataWithRange:NSMakeRange(0, nlRange.location)];
- // Remove data from the beginning up to and including the newline character:
- [collectedData replaceBytesInRange:NSMakeRange(0, nlRange.location + nlRange.length) withBytes:NULL length:0];
- // Process jsonData ...
- NSError *error;
- NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
- // ...
- NSString* type = [jsonDict objectForKey:@"type"];
- //NSLog(@"NEW NEW NEW --> %@n", jsonDict);
- NSString *outputData = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
- if(error) {
- NSLog(@"THE STRING ------------>>>>> %@n", outputData);
- NSLog(@"PARSE ERROR ------------->>>>> : %@n", error);
- }
- //NSLog(@"TYPEN ----------------------------------------------> %@", type);
- if([type isEqualToString:@"message"]) {
- //NSLog(@"New chat message: %@", output);
- [self messageReceived:outputData];
- } else if([type isEqualToString:@"offlineMessages"]) {
- NSLog(@"New offline messages: %@", outputData);
- NSLog(@"NEW OFFLINE MESSAGES!!");
- [self offlineMessagesReceived:outputData];
- }
- // Check for another newline character:
- nlRange =[collectedData rangeOfData:nl options:0 range:NSMakeRange(0, [collectedData length])];
- }
- }
- break;
- case NSStreamEventErrorOccurred:
- [self closeNetworkCommunication];
- isConnected = 0;
- NSLog(@"STREAM ERROR");
- //theStream = nil;
- break;
- case NSStreamEventEndEncountered:
- [self closeNetworkCommunication];
- isConnected = 0;
- NSLog(@"STREAM END");
- //theStream = nil;
- break;
- }
- }
- NSURLSession *session = [NSURLSession sharedSession];
- NSURLSessionDataTask *dataTask = [session dataTaskWithURL:myUrl completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
- NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
- NSLog(@"%@", json);
- }];
- [dataTask resume];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement