[英]ios writing fast to stream
循環以下代碼時遇到問題:
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
。
有沒有一種方法可以一次只發送一個response
字符串? 喜歡排隊嗎?
人們通常如何解決這個問題?
如果您“刷新”您的輸出流,那應該可以解決您的問題。 這是一個類似的問題,建議您在字符串中添加“ \\ n”或“ \\ r \\ n”以刷新它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.