我怎么能等到我使用NSStream

我怎么能等到我使用NSStream

问题描述:

case NSStreamEvent.EndEncountered: 
      BBLogMsg("End Encountered") 
      guard let inputStream = self.inputStream else { 
       return BBLogError("no input stream") 
      } 

      let bufferSize  = 4096 
      var buffer   = Array<UInt8>(count: bufferSize, repeatedValue: 0) 
      var message  = "" 

      while inputStream.hasBytesAvailable { 
       let len = inputStream.read(&buffer, maxLength: bufferSize) 
       if len < 0 { 
        BBLogError("error reading stream...") 
        return self.closeStreams() 
       } 
       if len > 0 { 
        message += NSString(bytes: &buffer, length: len, encoding: NSUTF8StringEncoding) as! String 
       } 
       if len == 0 { 
        BBLogError("no more bytes available...") 
        break 
       } 
      } 

      responseString += message 
      self.resolveReseponse() 

      self.closeStreams() 
      break 
     default: 
      break 
     } 
public func resolveReseponse() { 

    guard let data = responseString.dataUsingEncoding(NSUTF8StringEncoding) else { 
     return BBLogMsg("Server get disorted data") 
    } 
    do { 

     guard let _ = try NSJSONSerialization.JSONObjectWithData(data, options: []) as? [String: AnyObject] else { 

      return BBLogMsg("\n\n\n\nClient response string errorn\n\n\n\n\n\n") 
     } 

     self.didReceivedDataCallback?(responseString) 
     responseString = "" 
    } 
    catch 
    { 
     BBLogMsg("JSON Error Parser Issue \(responseString)") 
    } 
} 

我做了接收所有的TCP报文到客户端从服务器:如果服务器发送的任何数据到客户端,然后客户端直接验证JSON。如果客户端有任何损坏的JSON,则客户端将添加下一个数据包数据并进行验证。我怎么能等到我使用NSStream

我需要的:客户必须等待,直到它得到的所有数据,并做我从服务器得到了验证JSON数据

这不是迅速的,但我认为这种方法是相同的......

我目前使用这样的:

if (stream == inputStream) 
{ 
    uint8_t buffer[1024]; 

    int len = 0; 

    NSMutableString *output = [[NSMutableString alloc] init]; 

    while ([inputStream hasBytesAvailable]) 
    { 
     len = (int)[inputStream read:buffer maxLength:sizeof(buffer)]; 

     if (len > 0) 
     { 
      [output appendString: [[NSString alloc] initWithBytes:buffer length:len encoding:NSASCIIStringEncoding]]; 
     } 
    } 
    if (output.length > 0) 
    { 
     [self socketValidateResponse:output]; 
    } 
} 

这是用NSMutableString,只是附加分块数据。

里面-socketValidateResponse我有这个验证...

- (void)socketValidateResponse:(NSString *)stringResponse 
{ 
    stringResponse = stringResponse == nil ? @"" : stringResponse; 

    id socketJson = [NSJSONSerialization JSONObjectWithData:[stringResponse dataUsingEncoding:NSUTF8StringEncoding] 
                options:NSJSONReadingAllowFragments 
                 error:nil]; 

    BOOL isSupported = YES; 

    NSLog(@"Socket Log: -> \"%@\"", stringResponse); 

    if ([socketJson isKindOfClass:[NSArray class]]) 
    { 
     NSLog(@"Response is of class NSArray"); 
    } 
    else if ([socketJson isKindOfClass:[NSDictionary class]]) 
    { 
     NSLog(@"Response is of class NSDictionary"); 
    } 
    else //add more kind of class if necessary 
    { 
     isSupported = NO; 

     NSLog(@"Response is of class %@ not supported", [stringResponse class]); 
    } 

    if (isSupported) 
     //[self.delegate YJSocket:self didReceiveResponse:isSupported ? socketJson : nil]; 
} 

希望这将帮助..干杯! :)

+0

嘿@ 0yeoj我的情况是:我得到两个2000字节的数据包。我在做什么:我直接验证第一个数据包并验证第二个数据包。我的目标是:客户端应该等到所有数据包都收到。之后,它应该序列化我从服务器获得的数据 – Audi