Using LocalConnection to communicate between two Flex applications

早期采用计划的一些客户询问同一机器上两个Flex应用程序能否通信,答案是肯定的。LocalConnection类可让一个Flex应用向同一机器上的另一个Flex应用发送数据并触发事件,文中给出了简单示例及代码亮点说明。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Several Customers in the Early Adopter Program have asked me if two Flex applications running on the same machine can communicate with each other. The answer is yes: the LocalConnection class allows a Flex application to send data to- and trigger events in another Flex application running on the same machine. Here is a simple example:

1. "Sending" Application (Run it!)

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" initialize="initialize()">

    <mx:Script>

        var myConnection;

        function initialize() {
            myConnection = new LocalConnection();
            myConnection.onStatus = mx.utils.Delegate.create(this, onStatus);
        }

        function sendMessage() {
            myConnection.send("application_b", "messagePosted", myMessage.text);
        }

        function onStatus(result) {
            status.text=result.level == "error"?"Operation failed":"Operation succeeded";
        }

    </mx:Script>

    <mx:TextInput id="myMessage" />
    <mx:Button click="sendMessage()" label="Send"/>
    <mx:Label id="status"/>

</mx:Application>


Code highlights:

  • myConnection = new LocalConnection();
    Create an instance of the LocalConnection class
  • myConnection.onStatus = mx.utils.Delegate.create(this, onStatus);
    The onStatus event is triggered after the send() method is invoked to allow you to respond to the success or failure of the operation
  • myConnection.send("receivingapp", "messagePosted", myMessage.text);
    "reveivingapp" is the name the receiving app used to connect
    "messagePosted" is the event to trigger in the receiving application
    myMessage.text is the data to pass to the receiving application. You can pass objects or multiple comma-separated values.

2. "Receiving" Application ( Run it!)

<?xml version="1.0" encoding="utf-8"?>

<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" initialize="initialize()">

    <mx:Script>

        var myConnection;

        function initialize() {
            myConnection = new LocalConnection();
            myConnection.messagePosted = mx.utils.Delegate.create(this, messagePosted);
            myConnection.connect("receivingapp");
        }

        function messagePosted(message) {
            messageList.text+=message+"/n";
        }

    </mx:Script>

    <mx:TextArea id="messageList" width="300" height="300"/>

</mx:Application>


Code Highlights
  • myConnection = new LocalConnection();
    Create an instance of the LocalConnection class
  • myConnection.messagePosted = mx.utils.Delegate.create(this, messagePosted);
    Delegate messagePosted to the messagePosted function
  • myConnection.connect("receivingapp");
    Connect using "receivingapp" as the connection name

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值