我想在目标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