FLEX 新手 - 从 Java 转换异步通信
我目前正在将一个Java 项目转换为Flex 3。该项目的很大一部分涉及异步通信。
程序将连接到流,开始下载数据。 连接后不久,需要通过 HTTP 下载相同格式的上下文数据(关键帧)。 在流或关键帧期间的各个点,可能需要额外的 HTTP 调用,例如获取加密密钥。
在 Java 中,我将使用阻塞调用来执行 HTTP 请求以获取密钥,该密钥将在 HTTP 完成时有效地暂停关键帧或流,然后继续执行原来的操作。
不幸的是,Flex 是单线程的,并且 HTTP 请求是通过异步回调实现的,因此不可能以相同的方式实现代码。
作为 Flex 新手,我不确定通常的做法是什么。 目前,我计划保存状态并退出,依靠 HTTP 完成来重新启动暂停的流。 或者也许使用计时器......
但是对于流,我是否应该将接收到的数据与处理数据分离并在处理 HTTP 请求时缓冲它? 或者忽略进度事件并让 Flex 和/或操作系统缓冲它是否安全?
有人对可以使这一切变得更容易的架构有什么建议吗?
谢谢!
编辑:感谢您到目前为止的回答...
dirkgently - 我不确定我是否理解您的意思,但我会调查一下。
brd6644 - 连接之一(流)已经是原始套接字。 问题是如何暂停它并从中途从另一个来源获取一些其他数据。
CookieOfFortune - 看起来很有用,谢谢。
I'm currently translating a Java project to Flex 3. A large part of the project involves asynchronous communications.
The program will connect to a stream, start downloading data. Shortly after connecting, it will need to download context data in the same format (a keyframe) by HTTP. At various points during the stream or keyframe, additional HTTP calls may be required, e.g. to get an encryption key.
In Java, I'd use a blocking call to perform the HTTP requests to get the key which would effectively pause the keyframe or stream while the HTTP is completing, which would then carry on where it was.
Unfortunately, Flex being single-threaded, and HTTP requests being implemented with asynchronous call-backs, it's impossible to implement the code in the same way.
Being new to Flex, I'm not sure what the normal practice would be for this. Currently, I'm planning to save the state and exit, relying on the HTTP completion to re-start the paused stream. Or maybe use timers ...
But for the stream, should I decouple the data being received from the processing it and buffer it while the HTTP requests are being processed? Or is it safe to ignore the progress events and let Flex and/or the OS buffer it?
Does anyone have any advice on an architecture which would make all this easier?
Thanks!
EDIT: Thanks for the answers so far ...
dirkgently - I'm not sure I understand what you're getting at, but I'll look into it.
brd6644 - one of the connections (the stream) is already a raw socket. The question was about pausing it and grabbing some other data from another source mid-stream.
CookieOfFortune - that looks useful, thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Flex 有一个
HTTPService
< /a> 类。 从您的问题描述来看,我想说创建多个此类对象并将它们绑定到不同的事件处理程序 - 取决于您想要对特定请求进行的处理类型。最好围绕 HTTPService 创建一个自定义包装器并使用它们。 这将允许您轻松地使用自定义事件处理程序创建对象(并在完成后释放它们)。
Flex has a
HTTPService
class. From the looks of your problem description, I'd say create multiple such objects and bind them to different event handlers -- depending on the type of processing you want for a particular request.It is better to create a custom wrapper around the HTTPService and use these instead though. This will allow you to easily create objects with custom event handlers (and freeing them once done).
通过原始 ActionScript 套接字 API 进行连接怎么样? 您可以根据需要读取数据并分派事件,每个事件都会触发单独的 HTTPService 调用来获取数据。
What about connecting via the raw ActionScript socket API? You can read your data and dispatch events as needed, with each event triggering a separate HTTPService call to go get the data.
当您使用 HTTPService 的 send() 方法时,将返回 AsyncToken。 您可以使用 AsyncToken 来同步您的事件。
When you use the HTTPService's send() method, an AsyncToken is returned. You can use the AsyncToken to synchronize your events.