0

我尝试在 iPhone 上创建一个 Web 服务器来提供 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 立即关闭了第一个套接字。但为什么?

PS 顺便说一下,客户端使用 Android Volley 库来下载图像,而不是使用网络浏览器。

4

1 回答 1

0

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

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

于 2015-08-24T15:19:27.440 回答