使用 URLStream 在 AIR 中进行 Comet 式长轮询

发布于 2024-12-06 17:10:15 字数 1442 浏览 0 评论 0原文

我正在尝试使用 AIR 应用程序连接到现有的 Comet 式长轮询服务。该服务期望客户端发出带有 Connection: Keep-Alive 标头的 GET 请求。该请求将长时间保持打开状态,以便服务器可以推送数据。 在我的应用程序中,连接在 30 秒后因 IOError 终止。

这是 URLStream 未记录的限制吗?对 adl 的限制(我只通过 adl 运行我的应用程序)?

服务器不会向客户端发送任何“保持活动”消息,但不幸的是,这不是我可以控制的。

更新

为了测试这一点,我使用一个小的 php 脚本(由下面的 Leggetter 链接)设置了一个精简版本,并从一个简单的 AIR 应用程序中访问它。我发现无论我使用 URLStream 还是 URLLoader,我的连接都会在 30 秒后关闭。 PHP:

<?php
set_time_limit(0);

sleep(40);
echo("START!");

header('Content-type: text/plain');
echo str_pad('PADDING', 2048, '|PADDING');

$sleep_time = 1;
$count = 0;
while($count < 20) {
  echo($count);
  flush();
  $count = $count + 1;
  sleep($sleep_time);
}
echo("end");
?>

和 Actionscript:

private function beginSubscribeToNotifications():void {
    var req:URLRequest = new URLRequest(myPHPFile);
    req.method = URLRequestMethod.GET;
    req.requestHeaders.push( new URLRequestHeader("Connection", "Keep-Alive"));

    _urlLoader = new URLLoader();
    _urlLoader.addEventListener(Event.COMPLETE, onComplete); 
    _urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
    _urlLoader.load(req);
}

private function onComplete(e:Event):void {
    _message = (String)(_urlLoader.data);
}

如果我将 php 脚本中的初始睡眠时间调整为超过 30 秒,则会触发 IOError 事件。如果我降低睡眠时间,但请求在 30 秒后继续添加数据,则会调用 onComplete 事件,但 _urlLoader.data 为空。

此过程完全成功的唯一方法是整个过程在 30 秒内结束。

I'm attempting to connect to an existing Comet-style long-poll service using an AIR app. The service expects a client to make a GET request with a Connection: Keep-Alive header. This request will remain open for long periods of time so that the server can push data through.
In my app, the connection is terminated with an IOError after 30 seconds.

Is this an undocumented limitation of URLStream? A restriction on adl (I've only been running my app through adl)?

The server does not send any "keep-alive" messages to the client but, unfortunately this is not something i have control over.

Update

To test this, I've set up a stripped-down version using a little php script (linked by leggetter below) and am hitting it from a simple AIR app. I'm finding that my connections are closed after 30 seconds whether I use URLStream or URLLoader.
the PHP:

<?php
set_time_limit(0);

sleep(40);
echo("START!");

header('Content-type: text/plain');
echo str_pad('PADDING', 2048, '|PADDING');

$sleep_time = 1;
$count = 0;
while($count < 20) {
  echo($count);
  flush();
  $count = $count + 1;
  sleep($sleep_time);
}
echo("end");
?>

And the Actionscript:

private function beginSubscribeToNotifications():void {
    var req:URLRequest = new URLRequest(myPHPFile);
    req.method = URLRequestMethod.GET;
    req.requestHeaders.push( new URLRequestHeader("Connection", "Keep-Alive"));

    _urlLoader = new URLLoader();
    _urlLoader.addEventListener(Event.COMPLETE, onComplete); 
    _urlLoader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
    _urlLoader.load(req);
}

private function onComplete(e:Event):void {
    _message = (String)(_urlLoader.data);
}

If i adjust the initial sleep time in the php script to anything over 30 seconds, the IOError event is triggered. If I lower the sleep time, but the request continues adding data past 30 seconds, the onComplete event is called, but _urlLoader.data is empty.

The only way this process completely successfully is if the entire thing is over before 30 seconds elapses.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

筱果果 2024-12-13 17:10:15

好吧,这有点尴尬,但我想我应该发帖以防其他人遇到这种情况。
我通过设置 URLRequestDefaults.idleTimeout

根据文档:
当此属性设置为 0(默认值)时,运行时使用操作系统定义的默认空闲超时值。默认空闲超时值因操作系统(例如 Mac OS、Linux 或 Windows)以及操作系统版本而异。

我猜 Windows 7 的空闲超时值为 30 秒。

Well, this is somewhat embarrassing, but I figured I'd post in case someone else runs in to this.
I have solved my issue by setting the value of URLRequestDefaults.idleTimeout.

According to the documentation:
When this property is set to 0 (the default), the runtime uses the default idle timeout value defined by the operating system. The default idle timeout value varies between operating systems (such as Mac OS, Linux, or Windows) and between operating system versions.

I guess for Windows 7 it was 30 seconds.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文