-
Notifications
You must be signed in to change notification settings - Fork 503
iComet for iOS App
ideawu edited this page Dec 4, 2015
·
10 revisions
http://seiryu.home.comcast.net/~seiryu/libcurl-ios.html
Then add libcurl.a
and header files into your project, add the built-in libz.dylib
framework from xcode as well.
#include "curl/curl.h"
@interface ViewController (){
CURL *_curl;
}
@end
// this function is called in a separated thread, it gets called when receive msg from icomet server
size_t icomet_callback(char *ptr, size_t size, size_t nmemb, void *userdata){
const size_t sizeInBytes = size*nmemb;
NSData *data = [[NSData alloc] initWithBytes:ptr length:sizeInBytes];
NSString* s = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"%@", s);
ViewController *controller = (__bridge ViewController *)userdata;
dispatch_async(dispatch_get_main_queue(), ^{
// UI things
});
return sizeInBytes;
}
@implementation ViewController
- (void)viewDidLoad {
[self performSelectorInBackground:@selector(startStreaming) withObject:nil];
}
- (void)startStreaming{
_curl = curl_easy_init();
curl_easy_setopt(_curl, CURLOPT_URL, "http://127.0.0.1:8100/stream?cname=a&seq=1");
curl_easy_setopt(_curl, CURLOPT_NOSIGNAL, 1L); // try not to use signals
curl_easy_setopt(_curl, CURLOPT_USERAGENT, curl_version()); // set a default user agent
curl_easy_setopt(_curl, CURLOPT_WRITEFUNCTION, icomet_callback);
curl_easy_setopt(_curl, CURLOPT_WRITEDATA, self);
curl_easy_perform(_curl);
curl_easy_cleanup(_curl);
}
NSURLConnectionDelegate will buffer chunked data, so this will not work:
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data;