Flash Lite 2.x and 3.0 ActionScript Language Reference

onConnect (XMLSocket.onConnect handler)

onConnect = function(success:Boolean) {}

An asynchronous callback that the Flash Lite player invokes when a connection request initiated through XMLSocket.connect() succeeds or fails. If the connection succeeds, the success parameter has a value of true; otherwise the success parameter has a value of false.

The default implementation of this method performs no actions. To override the default implementation, you must assign a function containing custom actions.

Parameters

success:Boolean - A Boolean value indicating whether a socket connection is successfully established. If the connection succeeded, the success parameter has a value of true; otherwise the success parameter has a value of false.

Example

The following example illustrates the process of specifying a replacement function for the onConnect() event handler in a simple chat application.

After creating the XMLSocket object by using the constructor method, the script defines the custom function to be executed when the onConnect() event handler is invoked. The function controls the screen to which users are taken, depending on whether a connection is successfully established. If the connection is successfully made, users are taken to the main chat screen on the frame labeled startChat. If the connection is not successful, users go to a screen with troubleshooting information on the frame labeled connectionFailed.

var socket:XMLSocket = new XMLSocket();
socket.onConnect = function (success) {
    if (success) {
        gotoAndPlay("startChat");
    } else {
        gotoAndStop("connectionFailed");
    }
}

Now that the onConnect() handler is defined, the connect() method is invoked to attempt to establish the connection. If the connect() method returns a value of false, the SWF file is sent directly to the frame labeled connectionFailed, and onConnect() is never invoked. If the connect() method returns true, the SWF file jumps to a frame labeled waitForConnection, which is the "Please wait" screen. The SWF file remains on the waitForConnection frame until the onConnect() handler is invoked, which happens at some point in the future depending on network latency.

if (!socket.connect(null, 2000)) {
    gotoAndStop("connectionFailed");
} else {
    gotoAndStop("waitForConnection");
}

See also

connect (XMLSocket.connect method), Array function