0

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?

Alosyius
  • 8,771
  • 26
  • 76
  • 120

1 Answers1

1

If you 'flush' your output stream that should solve your problem. Here is a similar question that suggests you add "\n" or "\r\n" to your string to flush it.

Community
  • 1
  • 1
Ben Avery
  • 1,724
  • 1
  • 20
  • 33