我想在目標C中創建一個MJPEG查看器,但我遇到了一堆問題。創建一個MJPEG查看器iphone
首先,我使用AsyncSocket(http://code.google.com/p/cocoaasyncsocket/)讓我連接到主機。
這裏是我到目前爲止
NSLog(@"Ready");
asyncSocket = [[AsyncSocket alloc] initWithDelegate:self];
//http://kamera5.vfp.slu.se/axis-cgi/mjpg/video.cgi
NSError *err = nil;
if(![asyncSocket connectToHost:@"kamera5.vfp.slu.se" onPort:80 error:&err])
{
NSLog(@"Error: %@", err);
}
然後在didConnectToHost方法:
- (void)onSocket:(AsyncSocket *)sock didConnectToHost:(NSString *)host port:(UInt16)port{
NSLog(@"Accepted client %@:%hu", host, port);
NSString *urlString = [NSString stringWithFormat:@"http://kamera5.vfp.slu.se/axis-cgi/mjpg/video.cgi"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"GET"];
//set headers
NSString *_host = [NSString stringWithFormat:host];
[request addValue:_host forHTTPHeaderField: @"Host"];
NSString *KeepAlive = [NSString stringWithFormat:@"300"];
[request addValue:KeepAlive forHTTPHeaderField: @"Keep-Alive"];
NSString *connection = [NSString stringWithFormat:@"keep-alive"];
[request addValue:connection forHTTPHeaderField: @"Connection"];
//get response
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSString *result = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Response Code: %d", [urlResponse statusCode]);
if ([urlResponse statusCode] >= 200 && [urlResponse statusCode] < 300) {
NSLog(@"Response: %@", result);
//here you get the response
}
}
這將調用MJPEG流,但它不會調用它來獲得更多數據。我認爲它所做的只是加載第一部分數據,然後斷開連接。
我這樣做完全錯誤還是在隧道盡頭有光?
謝謝!
任何人都可以幫忙嗎?請...... – Tony 2010-02-08 16:37:47