Advertisement
Guest User

Untitled

a guest
Apr 19th, 2014
48
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.55 KB | None | 0 0
  1. - (void)stream:(NSStream *)theStream handleEvent:(NSStreamEvent)streamEvent {
  2.  
  3. collectedData = [NSMutableData data];
  4.  
  5. switch (streamEvent) {
  6.  
  7. case NSStreamEventOpenCompleted:
  8. NSLog(@"Stream opened");
  9.  
  10. break;
  11. case NSStreamEventHasBytesAvailable:
  12.  
  13. if (theStream == inputStream) {
  14.  
  15.  
  16. NSData *nl = [@"n" dataUsingEncoding:NSUTF8StringEncoding];
  17.  
  18. uint8_t bufferz[1024];
  19. int lenz;
  20. while ([inputStream hasBytesAvailable]) {
  21. lenz = [inputStream read:bufferz maxLength:sizeof(bufferz)];
  22. [collectedData appendBytes: (const void *)bufferz length:lenz];
  23. }
  24. NSRange nlRange =[collectedData rangeOfData:nl options:0 range:NSMakeRange(0, [collectedData length])];
  25. while (nlRange.location != NSNotFound) {
  26. // Extract data from the beginning up to (but not including) the newline character:
  27. NSData *jsonData = [collectedData subdataWithRange:NSMakeRange(0, nlRange.location)];
  28. // Remove data from the beginning up to and including the newline character:
  29. [collectedData replaceBytesInRange:NSMakeRange(0, nlRange.location + nlRange.length) withBytes:NULL length:0];
  30.  
  31. // Process jsonData ...
  32. NSError *error;
  33. NSMutableDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingMutableContainers error:&error];
  34. // ...
  35.  
  36.  
  37. NSString* type = [jsonDict objectForKey:@"type"];
  38.  
  39. //NSLog(@"NEW NEW NEW --> %@n", jsonDict);
  40. NSString *outputData = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
  41.  
  42.  
  43. if(error) {
  44. NSLog(@"THE STRING ------------>>>>> %@n", outputData);
  45. NSLog(@"PARSE ERROR ------------->>>>> : %@n", error);
  46. }
  47.  
  48.  
  49. //NSLog(@"TYPEN ----------------------------------------------> %@", type);
  50.  
  51. if([type isEqualToString:@"message"]) {
  52. //NSLog(@"New chat message: %@", output);
  53.  
  54. [self messageReceived:outputData];
  55.  
  56. } else if([type isEqualToString:@"offlineMessages"]) {
  57. NSLog(@"New offline messages: %@", outputData);
  58. NSLog(@"NEW OFFLINE MESSAGES!!");
  59. [self offlineMessagesReceived:outputData];
  60.  
  61. }
  62.  
  63. // Check for another newline character:
  64. nlRange =[collectedData rangeOfData:nl options:0 range:NSMakeRange(0, [collectedData length])];
  65. }
  66. }
  67. break;
  68.  
  69.  
  70. case NSStreamEventErrorOccurred:
  71.  
  72. [self closeNetworkCommunication];
  73. isConnected = 0;
  74.  
  75. NSLog(@"STREAM ERROR");
  76. //theStream = nil;
  77.  
  78. break;
  79.  
  80. case NSStreamEventEndEncountered:
  81.  
  82. [self closeNetworkCommunication];
  83. isConnected = 0;
  84.  
  85. NSLog(@"STREAM END");
  86. //theStream = nil;
  87.  
  88. break;
  89. }
  90. }
  91.  
  92. NSURLSession *session = [NSURLSession sharedSession];
  93. NSURLSessionDataTask *dataTask = [session dataTaskWithURL:myUrl completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
  94.  
  95. NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
  96. NSLog(@"%@", json);
  97. }];
  98. [dataTask resume];
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement