Table of Contents

LocalConnection.onStatus( ) Event Handler Flash 6

callback indicating initial status of a send( ) invocation
sendConn.onStatus(infoObject)

Arguments

infoObject

An object indicating the status of the most recent send( ) operation.

Description

The onStatus( ) event is triggered whenever LocalConnection.send( ) is invoked by sendConn. It tells us whether the send( ) operation found a valid connection that matched the specified connectionName. The onStatus( ) callback receives a single argument, infoObject, whose level property will have the value "error" if the specified connectionName was not found, or "status" if it was found. This handler is used for debugging and error-handling code. For example:

sendConn.onStatus = function (infoObject) {
  if (infoObject.level =  = "status") {
    trace("Connection found.");
  } else if (infoObject.level =  = "error") {
    trace("No connection found.");
    // ...handle the failed connection attempt...
  }
}

Even if infoObject.level is "status" (a connection is found), the connection may not be accepted by the receiver, and the receiver may not define the specified method. For information on allowing cross-domain connections, see LocalConnection.allowDomain( ). To verify that a method was successfully invoked, the receiver must call a reply method on the sender, as shown in the following Example.

Example

The following code shows a sender object that invokes displayMsg( ) on a receiver object. The receiver object responds by invoking methodReturn( ) on the sender. If no connection is found, the sender displays a debugging message. Notice how each LocalConnection object acts as both a sender and a receiver, but we'll refer to the primary sender as "the sender" and the primary receiver as "the receiver" for convenience. In the following example, the remote invocation is transacted over "channelA" (which is opened by the receiver and accessed by the sender), and the response is transmitted over "channelB" (which is opened by the sender and accessed by the receiver). This relies on both the sender and receiver agreeing on the channel names ahead of time. Furthermore, it assumes that both the sending and receiving movies are hosted on the same subdomain. Refer to the Discussion under LocalConnection.send( ) for dealing with senders and receivers hosted on different domains.

Here is the code in the sender:

// Create a text field to display messages
this.createTextField("sendOutput_txt", 1, 50, 100, 400, 100);
sendOutput_txt.border = true;
   
// Create the sender LocalConnection object
sendConn = new LocalConnection();
// Create a method to catch receiver method return values
sendConn.methodReturn = function (result) {
  sendOutput_txt.text += result + "\n";
}
   
// Attempt to open a connection named "channelB" to be used for the response
connectSuccess = sendConn.connect("channelB");
   
// Display whether the connection succeeded
sendOutput_txt.text = "Sender connection succeeded: " + connectSuccess + "\n";
   
// Invoke displayMsg() remotely using "channelA",
// which hopefully the receiver opened
sendConn.send("channelA", "displayMsg", "hello world");
   
// Check whether the connection is available
sendConn.onStatus = function (infoObject) {
  if (infoObject.level =  = "status") {
    sendOutput_txt.text += "Connection found.\n";
  } else if (infoObject.level =  = "error") {
    sendOutput_txt.text += "No connection found.\n";
  }
}

And here is the code in the receiver:

// Create a text field to display messages
this.createTextField("receiveOutput_txt", 1, 50, 100, 400, 100);
receiveOutput_txt.border = true;
   
// Create the LocalConnection object
receiveConn = new LocalConnection();
   
// Assign a method to be invoked remotely
receiveConn.displayMsg = function (msg) {
  receiveOutput_txt.text += msg + "\n";
  // Let the sender know the method was called. Response is sent on "channelB"
  this.send("channelB", "methodReturn", "displayMsg() called");
}
   
// Attempt to open a connection named "channelA" to be used to receive messages
connectSuccess = receiveConn.connect("channelA");
   
// Display whether the connection succeeded
receiveOutput_txt.text = "Receiver connection succeeded: " + connectSuccess + "\n";

See Also

LocalConnection.allowDomain( ), LocalConnection.connect( ), LocalConnection.domain( ), LocalConnection.send( )


Table of Contents