循环下面的代码时遇到问题:
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
字符串?像队列一样?
人们通常如何解决这个问题?