我的应用程序想要运行一个 http 服务器,其他设备可以从中下载文件。
我正在使用可可 http 服务器并遵循:https ://github.com/robbiehanson/CocoaHTTPServer/blob/master/Samples/DynamicServer/MyHTTPConnection.m
我的http连接是:
@implementation MyHTTPConnection
- (NSObject<HTTPResponse> *)httpResponseForMethod:(NSString *)method URI:(NSString *)path
{
NSString *filePath = [self filePathForURI:path];
// Convert to relative path
NSString *documentRoot = [config documentRoot];
NSString *relativePath = [filePath substringFromIndex:[documentRoot length]];
NSLog(@"file path is %@", filePath);
//return [super httpResponseForMethod:method URI:path];
return [[HTTPFileResponse alloc] initWithFilePath:filePath forConnection:self];
}
@end
Delegate 中的代码是:
httpServer = [[HTTPServer alloc] init];
[httpServer setConnectionClass:[MyHTTPConnection class]];
[httpServer setType:@"_http._tcp."];
NSString *webPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"web"];
if(webPath){
NSString * ip = '192.168.1.10';
int port = 3321;
[httpServer setDocumentRoot:webPath];
[httpServer setPort:port];
[httpServer setInterface: ip];
[self startServer];
}
当我下载 web 文档中的测试文件时,大小始终为 o 字节。