循环以下代码时遇到问题:

    NSString *response  = [NSString stringWithFormat:@"{\"id\":\"%@\",\"type\":\"%@\",\"msg\":\"%@\"}",userID, method, msg];
    NSData *data = [[NSData alloc] initWithData:[response dataUsingEncoding:NSASCIIStringEncoding]];
    [outputStream write:[data bytes] maxLength:[data length]];


这是我打开流的方式:

// Open connection to server
- (void)initNetworkCommunication {
    isConnected = TRUE;
    CFReadStreamRef readStream;
    CFWriteStreamRef writeStream;
    CFStreamCreatePairWithSocketToHost(NULL, (CFStringRef)@"url.com", 8080, &readStream, &writeStream);
    inputStream = (__bridge NSInputStream *)readStream;
    outputStream = (__bridge NSOutputStream *)writeStream;

    [inputStream setDelegate:self];
    [outputStream setDelegate:self];

    [inputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
    [outputStream scheduleInRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

    [inputStream open];
    [outputStream open];
}


问题在于,当快速写入流时,它将所有消息合并为一个,我的服务器当时只能解析一个JSON

有没有一种方法可以一次只发送1个response字符串?喜欢排队吗?

人们通常如何解决这个问题?

最佳答案

如果您“刷新”您的输出流,那应该可以解决您的问题。 Here是一个类似的问题,建议您在字符串中添加“ \ n”或“ \ r \ n”以刷新它。

关于ios - iOS快速写入流,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19650868/

10-09 02:21