客户端无法从 gcdwebserver 接收图像数据

Client cannot receive image data from gcdwebserver

我尝试在 iPhone 上创建一个网络服务器来提供 ablum 中的图像 url。 在向客户端发送响应之前,我需要对图像进行一些调整(比如调整大小)。 在响应客户端之前,这些操作每张图像最多需要 1 或 1.5 秒。 (尤其是 iPhone 4) 即使 xcode 控制台上的消息已经显示在某些套接字上已获取了多少字节,客户端也无法接收数据。 我可以使用哪些选项来解决这个问题?

另一个问题是,如果我将 ConnectedStateCoalescingInterval 值增加到 2.0 或 3.0 秒会发生什么情况?

谢谢。

========= 2015/08/24 13:05 更新

这是我的addHandlerForMethod:path: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 库下载图像而不是使用网络浏览器。

根据日志似乎没有任何错误:2 GET 路径 /image/9D959040-9FA8-4197-8150-8DC72339955D 请求在套接字 32 和 34 上处理,每个 return 123,575字节。

尝试使用 Chrome 之类的桌面网络浏览器及其网络检查器。如果一切正常,那么问题出在您的 Android 应用上。