I'm having problems when looping the code below:
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]];
This is how i open the stream:
// 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];
}
The problem is that when writing to the stream fast it combines all messages into one, my server can only parse one JSON
at the time.
Is there a way to implement that only 1 response
string is sent at a time? Like a queue?
How do people usually solve this?