[ActionScritp 3.0] 使用LocalConnection建立通信

 

flash.net
public class LocalConnection
继承 LocalConnection → EventDispatcher → Object

 

语言版本:  ActionScript 3.0
运行时版本:  AIR 1.0, Flash Player 9, Flash Lite 4

使用 LocalConnection 类可以创建调用另一个 LocalConnection 对象中的方法的 LocalConnection 对象。通信可以通过以下方式进行:

  • 在一个 SWF 文件内
  • 在多个 SWF 文件之间
  • 在 AIR 应用程序的内容(基于 SWF 或基于 HTML)之间
  • 在 AIR 应用程序的内容(基于 SWF 或基于 HTML)和运行于浏览器中的 SWF 内容之间

AIR 配置文件支持:所有桌面操作系统和所有 AIR for TV 设备均支持此功能,但移动设备不支持此功能。您可以使用 LocalConnection.isSupported 属性在运行时测试是否受支持。有关在多个配置文件之间支持 API 的详细信息,请参阅 AIR 配置文件支持

注意:AIR for TV 设备仅支持 AIR 应用程序中基于 SWF 的内容之间的通信。

通过本地连接,可以在 SWF 文件之间进行这种通信,而不用使用 fscommand() 或 JavaScript。LocalConnection 对象只能在运行于同一台客户端计算机上的文件之间进行通信,但这些文件可以在不同的应用程序中运行。例如,浏览器中运行的文件和 Adobe AIR 中运行的 SWF 文件。

ActionScript 3.0 中创建的 LocalConnection 对象可以与 ActionScript 1.0 或 2.0 中创建的 LocalConnection 对象通信。反之亦然:ActionScript 1.0 或 2.0 中创建的 LocalConnection 对象可以与 ActionScript 3.0 中创建的 LocalConnection 对象通信。Flash Player 可自动处理不同版本 LocalConnection 对象间的通信。

可以使用三种方式将回调方法添加到 LocalConnection 对象:

  • 使 LocalConnection 类成为子类,并添加方法。
  • LocalConnection.client 属性设置为实现方法的对象。
  • 创建扩展 LocalConnection 的动态类,并动态附加方法。

为了了解如何使用 LocalConnection 对象在两个文件之间进行通信,了解每个文件中使用的命令非常有用。一个文件被称为接收方文件;该文件中包含要调用的方法。接收方文件中必须包含一个 LocalConnection 对象和对 connect() 方法的调用。另一个文件被称为发送方文件;这是调用方法的那个文件。发送方文件中必须包含另一个 LocalConnection 对象和对 send() 方法的调用。

send()connect() 的使用将有所不同,这取决于 文件是在同一个域中、在具有可预知域名的不同域中还是在具有不可预知域名(即动态域名)的不同域中。下文将说明这三种不同的情况,并针对每种情况分别提供代码示例。

同一个域。这是使用 LocalConnection 对象最简单的情况,它只允许在位于同一个域中的 LocalConnection 对象间通信,这是因为默认情况下,应用程序允许同域通信。当同一个域中的两个 文件通信时,无需实施任何特殊的安全措施,而只需将 connectionName 参数的同一个值传递给 connect()send() 方法。

 

// receivingLC is in http://www.domain.com/receiving.swf
receivingLC.connect('myConnection');

// sendingLC is in http://www.domain.com/sending.swf
// myMethod() is defined in sending.swf
sendingLC.send('myConnection', 'myMethod');

具有可预知域名的不同域。当不同域中的两个 SWF 文件通信时,需要通过调用 allowDomain() 方法来允许在这两个不同域之间进行通信。还需要在 send() 方法中使用接收方 LocalConnection 对象的域名限定连接名:

// receivingLC is in http://www.domain.com/receiving.swf
receivingLC.allowDomain('www.anotherdomain.com');
receivingLC.connect('myConnection');

// sendingLC is in http://www.anotherdomain.com/sending.swf
sendingLC.send('www.domain.com:myConnection', 'myMethod');

具有不可预知域名的不同域。有时候,可能希望具有接收方 LocalConnection 对象的 文件在域之间具有更好的可移植性。为了避免在 send() 方法中指定域名,但要指出接收方和发送方 LocalConnection 对象不在同一个域中,可在 connect()send() 调用中的连接名称之前加一个下划线 (_)。要允许在这两个不同域之间通信,请调用 allowDomain() 方法并传递您希望允许 LocalConnection 调用的域。或者,也可以传递通配符 (*) 参数来允许从所有域调用:

// receivingLC is in http://www.domain.com/receiving.swf
receivingLC.allowDomain('*');
receivingLC.connect('_myConnection');

// sendingLC is in http://www.anotherdomain.com/sending.swf
sendingLC.send('_myConnection', 'myMethod');

从 Flash Player 到 AIR 应用程序。在 AIR 应用程序沙箱中创建的 LocalConnection 对象使用一个特殊字符串作为连接前缀,而不是使用域名。此字符串的格式为 app#appID.pubID,其中,appID 是应用程序 ID,pubID 是应用程序的发行商 ID。(如果 AIR 应用程序使用发行商 ID,则仅包括发行商 ID。)例如,如果 AIR 应用程序的应用程序 ID 为“com.example”,没有发行商 ID,则您可以使用 app#com.example:myConnection 作为本地连接字符串。AIR 应用程序还必须调用 allowDomain() 方法,传入调用 SWF 文件的原始域:

// receivingLC is an AIR application with app ID = com.example (and no publisher ID)
receivingLC.allowDomain('www.domain.com');
receivingLC.connect('myConnection');

// sendingLC is in http://www.domain.com/sending.swf
sendingLC.send('app#com.example:myConnection', 'myMethod');

注意:如果 AIR 应用程序在 AIR 应用程序沙箱外部加载 SWF,则使用该 SWF 建立本地连接的规则与使用 Flash Player 中运行的 SWF 建立连接的规则相同。

从 AIR 应用程序到 Flash Player。当 AIR 应用程序与 Flash Player 运行时中运行的 SWF 通信时,您需要通过调用 allowDomain() 方法并传入 AIR 应用程序的连接前缀,来允许两者之间的通信。例如,如果 AIR 应用程序的应用程序 ID 为“com.example”,没有发行商 ID,则您可以将字符串 app#com.example 传递给 allowDomain() 方法。您还需要在 send() 方法中使用接收方 LocalConnection 对象的域名(使用“localhost”作为从本地文件系统加载的 SWF 文件的域)来限定连接名:

// receivingLC is in http://www.domain.com/receiving.swf
receivingLC.allowDomain('app#com.example');
receivingLC.connect('myConnection');

// sendingLC is an AIR application with app ID = com.example (and no publisher ID)
sendingLC.send('www.domain.com:myConnection', 'myMethod');

从一个 AIR 应用程序到另一个 AIR 应用程序。要在两个 AIR 应用程序之间进行通信,您需要通过调用 allowDomain() 方法并传入发送方 AIR 应用程序的连接前缀,来允许两者之间的通信。例如,如果发送方应用程序的应用程序 ID 为“com.example”,没有发行商 ID,您可以将字符串 app#com.example 传递给接收方应用程序中的 allowDomain() 方法。还需要在 send() 方法中使用接收方 LocalConnection 对象的连接前缀限定连接名:

// receivingLC is an AIR application with app ID = com.sample (and no publisher ID)
receivingLC.allowDomain('app#com.example');
receivingLC.connect('myConnection');

// sendingLC is an AIR application with app ID = com.example (and no publisher ID)
sendingLC.send('app#com.sample:myConnection', 'myMethod');

可以使用 LocalConnection 对象发送和接收单个 文件中的数据,但这不是通常的用法。

有关 send()connect() 方法的详细信息,请参阅 LocalConnection.send()LocalConnection.connect() 条目中对 connectionName 参数的讨论。此外,请参阅 allowDomain()domain 条目。

posted on 2016-03-21 14:21  晏过留痕  阅读(555)  评论(0编辑  收藏  举报