1

What I am trying to do: I have a url request (post) where I send some information to a api server which then starts streaming data in bytes to me.

1) How do I post data when trying to set up a stream as right now I am just using a url, can I incorporate a NSURLRequest some how?

2) Why isnt my stream even opening (streamStatus returns 0) and thus - (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode never being called? , this is my best attempt and for the most part following this Guide

- (void)setUpStreamFromURL:(NSURL *)path {

     // iStream is NSInputStream instance variable
     iStream = [[NSInputStream alloc] initWithURL:path];
     [iStream setDelegate:self];
     [iStream scheduleInRunLoop:[NSRunLoop currentRunLoop]
                   forMode:NSDefaultRunLoopMode];
     [iStream open];

     NSLog(@"Stream Open: %lu",[iStream streamStatus]); //return 0
}

- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode {
     NSLog(@"Streaming");
     switch(eventCode) {
         case NSStreamEventHasBytesAvailable:
         {
             if(!_data) {
                  _data = [[NSMutableData data] init];
             }
              uint8_t buf[1024];
              unsigned int len = 0;
              len = [(NSInputStream *)stream read:buf maxLength:1024];
              if(len) {
              [_data appendBytes:(const void *)buf length:len];
              NSLog(@"DATA BEING SENT : %@", _data);
              // bytesRead is an instance variable of type NSNumber.
             // [bytesRead setIntValue:[bytesRead intValue]+len]; //getting error that setInt value is not part of NSNumber, and thats true so not sure what to do about it, but this isn't the issue. 
              } else {
                    NSLog(@"no buffer!");
              } 
              break;
         }
         case NSStreamEventEndEncountered:
         {
              [stream close];
              [stream removeFromRunLoop:[NSRunLoop currentRunLoop]
                          forMode:NSDefaultRunLoopMode];
              stream = nil; // stream is ivar, so reinit it
              break;
         }
        // continued ...
     }
}

also incase it helps, my header file:

 #import <Foundation/Foundation.h>
 #import "Login.h"

 @interface Stream : NSStream <NSStreamDelegate> {
NSMutableArray *searchIdList;
NSInputStream *iStream;
NSNumber *bytesRead;

}
- (void)stream:(NSStream *)stream handleEvent:(NSStreamEvent)eventCode ;
-(id)initWithLoginObject:(Login *)log;
@property NSMutableData *data;
@end
Osman
  • 1,771
  • 4
  • 24
  • 47

1 Answers1

0

You can't use NSURLRequest in Stream.

To create a request you can use this.

request = CFHTTPMessageCreateRequest(kCFAllocatorDefault, CFSTR("POST"),(CFURLRef) url, kCFHTTPVersion1_1);
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("User-Agent"), CFSTR("MSControl-US"));
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Content-Type"), CFSTR("application/json"));
CFHTTPMessageSetHeaderFieldValue(request, CFSTR("Connection"), CFSTR("Keep-Alive"));

After that you can create the stream using

 readStream = CFReadStreamCreateForHTTPRequest(kCFAllocatorDefault, request)

or if you already have an opened stream you can serialize your request With :

NSData *data = (NSData *)CFHTTPMessageCopySerializedMessage(request);

and send with This:

length = [data length];
CFWriteStreamWrite((CFWriteStreamRef)outStream,data,lenght);

Hope this help

thelastworm
  • 544
  • 4
  • 14
  • I have put this project aside for a while now but I will revisit it this weekend and see, Ill mark it as answer or comment back accordingly. – Osman Apr 11 '13 at 13:55