我嘗試在iPhone上創建Web服務器以提供ablum中的圖像網址。 在向客戶端發送響應之前,我需要對圖像進行一些調整(如重新調整大小)。 這些操作在響應客戶端之前每個圖像需要1或1.5秒。 (特別是在iPhone 4上) 即使xcode控制檯上的消息已經顯示某些套接字上有多少字節已被GET,客戶端仍無法接收數據。 我可以用什麼方法來解決這個問題?客戶端無法接收來自gcdwebserver的圖像數據
另一個問題是如果我將ConnectedStateCoalescingInterval值增加到2.0或3.0秒會發生什麼?
謝謝。
========= 2015年8月24日13:05更新
這裏是我的addHandlerForMethod:路徑:requestClass:asyncProcessBlock:
[_webServer addHandlerForMethod:@"GET" path:[NSString stringWithFormat:@"/image/%@",assetId] requestClass:[GCDWebServerRequest class] asyncProcessBlock:^(GCDWebServerRequest *request, GCDWebServerCompletionBlock completionBlock)
{
__strong typeof(weakself) strongself = weakself;
[strongself requestImageDataByAssetURL:assetURL completionCb:^(NSData *photoData) {
GCDWebServerDataResponse* response = [GCDWebServerDataResponse responseWithData:photoData contentType:@"image/png"];
completionBlock(response);
}];
}];
這裏是requestImageDataByAssetURL:completionCb:
- (void)requestImageDataByAssetURL:(NSURL *)assetURL completionCb:(void(^)(NSData *photoData))cbfunc {
__block NSData *photoData;
ALAssetsLibrary* library = [[ALAssetsLibrary alloc] init];
[library assetForURL:assetURL resultBlock:^(ALAsset *asset) {
@autoreleasepool {
uint64_t begin = mach_absolute_time();
// <do some image processing here>
uint64_t end = mach_absolute_time();
NSLog(@"Time taken to imageResize %g s", MachTimeToSecs(end - begin));
cbfunc(photoData);
}
} failureBlock:^(NSError *error) {
NSLog(@"[%@] fail. Error=%@", NSStringFromSelector(_cmd), error.localizedDescription);
cbfunc(nil);
}];
}
這裏是日誌:
[DEBUG] [2015-08-24 04:48:09 +0000] Did open connection on socket 32
[DEBUG] Connection received 221 bytes on socket 32
[DEBUG] Connection on socket 32 preflighting request "GET /image/9D959040-9FA8-4197-8150-8DC72339955D" with 221 bytes body
[DEBUG] Connection on socket 32 processing request "GET /image/9D959040-9FA8-4197-8150-8DC72339955D" with 221 bytes body
[DEBUG] [2015-08-24 04:48:10 +0000] Did open connection on socket 34
2015-08-24 12:48:10.799 xBoard[2981:3707] Time taken to imageResize 1.52039 s
[DEBUG] Connection received 221 bytes on socket 34
[DEBUG] Connection on socket 34 preflighting request "GET /image/9D959040-9FA8-4197-8150-8DC72339955D" with 221 bytes body
[DEBUG] Connection on socket 34 processing request "GET /image/9D959040-9FA8-4197-8150-8DC72339955D" with 221 bytes body
[DEBUG] Connection sent 171 bytes on socket 32
[DEBUG] Connection sent 123575 bytes on socket 32
[DEBUG] Did close connection on socket 32
[VERBOSE] [172.16.20.174:8080] 172.16.21.97:34450 200 "GET /image/9D959040-9FA8-4197-8150-8DC72339955D" (221 | 123746)
2015-08-24 12:48:12.028 xBoard[2981:4f0b] Time taken to imageResize 1.06382 s
[DEBUG] Connection sent 171 bytes on socket 34
[DEBUG] Connection sent 123575 bytes on socket 34
[DEBUG] Did close connection on socket 34
[VERBOSE] [172.16.20.174:8080] 172.16.21.97:50852 200 "GET /image/9D959040-9FA8-4197-8150-8DC72339955D" (221 | 123746)
我們可以看到它創建了第一個套接字(32)。
然後立即爲相同的圖像請求創建第二個套接字(34)。
看來,GCDWebServer立即關閉第一個套接字。但爲什麼?
P.S.順便說一下,客戶端使用Android Volley庫來下載圖片,而不是使用網絡瀏覽器。
在考慮HTTP請求無響應之前,瀏覽器通常會允許至少30秒。 GCDWEBServer本身不太可能出現問題。你能添加一些簡化的示例代碼和一些日誌輸出嗎? – Pol