Adobe AIR HTTP 连接限制
我正在开发一个 Adobe AIR 应用程序,它可以将文件上传到运行 Apache 和 PHP 的 Web 服务器。可以同时上传多个文件,应用程序还可以调用 Web 服务器来执行各种 API 请求。
我遇到的问题是,如果我开始两个文件上传,当它们正在进行时,任何其他 HTTP 请求都会超时,从用户的角度来看,这会导致应用程序出现问题。
Adobe AIR 应用程序是否仅限于 2 个 HTTP 连接,还是可能存在其他问题? 通过搜索这个问题,我没有发现太多,但一篇文章确实表明它不仅限于两个连接。
文件上传是通过调用 File 类的 upload 方法来执行的,API 调用是使用 HTTPService 类来完成的。我使用的开发 Web 服务器是 WAMP 服务器,但是当应用程序发布时,它将与 LAMP 服务器通信。
谢谢, 以下
是我用来上传文件的代码:
protected function btnAddFile_clickHandler(event:MouseEvent):void
{
// Create a new File object and display the browse file dialog
var uploadFile:File = new File();
uploadFile.browseForOpen("Select File to Upload");
uploadFile.addEventListener(Event.SELECT, uploadFile_SelectedHandler);
}
private function uploadFile_SelectedHandler(event:Event):void
{
// Get the File object which was used to select the file
var uploadFile:File = event.target as File;
uploadFile.addEventListener(ProgressEvent.PROGRESS, file_progressHandler);
uploadFile.addEventListener(IOErrorEvent.IO_ERROR, file_ioErrorHandler);
uploadFile.addEventListener(Event.COMPLETE, file_completeHandler);
// Create the request URL based on the download URL
var requestURL:URLRequest = new URLRequest(AppEnvironment.instance.serverHostname + "upload.php");
requestURL.method = URLRequestMethod.POST;
// Set the post parameters
var params:URLVariables = new URLVariables();
params.name = "filename.ext";
requestURL.data = params;
// Start uploading the file to the server
uploadFile.upload(requestURL, "file");
}
以下是 API 调用的代码:
private function sendHTTPPost(apiFile:String, postParams:Object, resultCallback:Function, initialCallerResultCallback:Function):void
{
var httpService:mx.rpc.http.HTTPService = new mx.rpc.http.HTTPService();
httpService.url = AppEnvironment.instance.serverHostname + apiFile;
httpService.method = "POST";
httpService.requestTimeout = 10;
httpService.resultFormat = HTTPService.RESULT_FORMAT_TEXT;
httpService.addEventListener("result", resultCallback);
httpService.addEventListener("fault", httpFault);
var token:AsyncToken = httpService.send(postParams);
// Add the initial caller's result callback function to the token
token.initialCallerResultCallback = initialCallerResultCallback;
}
I'm working on an Adobe AIR application which can upload files to a web server, which is running Apache and PHP. Several files can be uploaded at the same time and the application also calls the web server for various API requests.
The problem I'm having is that if I start two file uploads, while they are in progress any other HTTP requests will time out, which is causing a problem for the application and from a user point of view.
Are Adobe AIR applications limited to 2 HTTP connections, or is something else probably the issue?
From searching about this issue I've not found much but one article did indicated that it wasn't limited to just two connections.
The file uploads are performed by calling the File classes upload method, and the API calls are done using the HTTPService class. The development web server I am using is a WAMP server, however when the application is released it will be talking to a LAMP server.
Thanks,
Grant
Here is the code I'm using to upload the file:
protected function btnAddFile_clickHandler(event:MouseEvent):void
{
// Create a new File object and display the browse file dialog
var uploadFile:File = new File();
uploadFile.browseForOpen("Select File to Upload");
uploadFile.addEventListener(Event.SELECT, uploadFile_SelectedHandler);
}
private function uploadFile_SelectedHandler(event:Event):void
{
// Get the File object which was used to select the file
var uploadFile:File = event.target as File;
uploadFile.addEventListener(ProgressEvent.PROGRESS, file_progressHandler);
uploadFile.addEventListener(IOErrorEvent.IO_ERROR, file_ioErrorHandler);
uploadFile.addEventListener(Event.COMPLETE, file_completeHandler);
// Create the request URL based on the download URL
var requestURL:URLRequest = new URLRequest(AppEnvironment.instance.serverHostname + "upload.php");
requestURL.method = URLRequestMethod.POST;
// Set the post parameters
var params:URLVariables = new URLVariables();
params.name = "filename.ext";
requestURL.data = params;
// Start uploading the file to the server
uploadFile.upload(requestURL, "file");
}
Here is the code for the API calls:
private function sendHTTPPost(apiFile:String, postParams:Object, resultCallback:Function, initialCallerResultCallback:Function):void
{
var httpService:mx.rpc.http.HTTPService = new mx.rpc.http.HTTPService();
httpService.url = AppEnvironment.instance.serverHostname + apiFile;
httpService.method = "POST";
httpService.requestTimeout = 10;
httpService.resultFormat = HTTPService.RESULT_FORMAT_TEXT;
httpService.addEventListener("result", resultCallback);
httpService.addEventListener("fault", httpFault);
var token:AsyncToken = httpService.send(postParams);
// Add the initial caller's result callback function to the token
token.initialCallerResultCallback = initialCallerResultCallback;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您使用的是 Windows 系统,Adobe AIR 将使用 Microsoft 的 WinINet 库 访问网络。默认情况下,此库将与单个服务器的并发连接数限制为 2 :
有一个 API 可以更改此限制的值,但我不知道是否可以从 AIR 访问它。
由于此限制还会影响网站的页面加载速度,因此某些网站对图像、JavaScript 和样式表等工件使用多个 DNS 名称,以允许浏览器打开更多并行连接。
因此,如果您控制服务器部分,解决方法可能是创建 DNS 别名,例如用于上传的
www.example.com
和用于 API 请求的api.example.com
。If you are on a windows system, Adobe AIR is using Microsofts WinINet library to access the web. This library by default limits the number of concurrent connections to a single server to 2:
There is an API to change the value of this limit but I don't know if it is accessible from AIR.
Since this limit also affects page loading speed for web sites, some sites are using multiple DNS names for artifacts such as images, javascripts and stylesheets to allow a browser to open more parallel connections.
So if you are controlling the server part, a workaround could be to create DNS aliases like
www.example.com
for uploads andapi.example.com
for API requests.因此,当我研究这个问题时,我在文档中发现了有关使用 File.upload() 的信息:
我想知道上传多个文件时是否会出现问题。我发现您正在使用 browserForOpen() 而不是 browser()。看起来他们可能会做同样的事情......但也许不是。
我还在 File 类文档中看到了这一点
当您说允许用户上传多个文件时,您是指对 browser() 和 upload() 的后续调用,还是指包含多个文件的一次调用?看来,如果您尝试进行多个单独的调用,这可能是一个问题。
无论如何,我不知道这是否有很大帮助。看来您正在尝试做的事情应该是可能的。我只能猜测,问题可能出在执行上。祝你好运:)
参考:http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html#upload()
http://help.adobe .com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html#browse()
So as I was looking into this, I came across this info about using File.upload() in the documentation:
I wonder if something there could be giving you issues with uploading multiple files. I see that you are using browserForOpen() instead of browse(). It seems like the probably do the same thing... but maybe not.
I also saw this in the File class documentation
When you say that you let users upload multiple files, do you mean subsequent calls to browse() and upload() or do you mean one call that includes multiple files? It seems that if you are trying to do multiple separate calls that that may be an issue.
Anyway, I don't know if this is much help. It definitely seems that what you are trying to do should be possible. I can only guess that what is going wrong is perhaps a problem with implementation. Good luck :)
Reference: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html#upload()
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html#browse()
仅仅因为我正在考虑一个非常相似的问题,因为我的一个实际应用程序出现错误,所以我决定写下我找到的答案。
我实例化了11
并想知道为什么我的 Flex 4 应用程序停止工作并抛出 HTTP 错误,尽管它以前工作得很好,只有 5 个同时的 HttpConnections 到同一台服务器。
我自己对此进行了测试,因为我在 Flex 文档或互联网上没有找到任何与此相关的内容。
我发现使用超过 5 个 HTTPConnections 是 Flex 应用程序抛出运行时错误的原因。
我决定一个接一个地实例化连接,作为一种临时解决方法:在另一个连接接收到数据后加载下一个连接,依此类推。
当然,这只是暂时的,因为接下来的步骤之一将是更改响应服务器代码,使其应答包含在一次响应中对多个表的请求结果的请求。当然,客户端应用程序逻辑也需要更改。
Just because I was thinking about a very similar question because of an error in one of my actual apps, I decided to write down the answer I found.
I instantiated 11
and was wondering why my Flex 4 Application stopped working and threw an HTTP-Error although it was working pretty good formerly with just 5 simultanious HttpConnections to the same server.
I tested this myself because I did not find anything regarding this in the Flex docs or on the internet.
I found that using more than 5 HTTPConnections was the reason for the Flex application to throw the runtime error.
I decided to instantiate the connections one after another as a temporally workaround: Load the next one after the other has received the data and so on.
Thats of course just temporally since one of the next steps will be to alter the responding server code in that way that it answers a request that contains the results of requests to more then one table in one respond. Of course the client application logic needs to be altered, too.