# XMLSocket.onConnect()

#### Availability

Flash Player 5.

#### Usage

`myXMLSocket``.onConnect(``success``)`

#### Parameters

`success` A Boolean value indicating whether a socket connection was successfully established (`true` or `false`).

#### Returns

Nothing.

#### Description

Event handler; a callback function invoked by the Flash Player when a connection request initiated through the XMLSocket.connect() method has succeeded or failed. If the connection succeeded, the `success` parameter is `true`; otherwise the `success` parameter is `false`.

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

#### Example

The following example illustrates the process of specifying a replacement function for the `onConnect` method in a simple chat application.

The function controls which screen users are taken to, 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`.

    function myOnConnect(success) {
    	if (success) {
    		gotoAndPlay("startChat")
    	} else {
    		gotoAndStop("connectionFailed")
    	}
    }

After creating the XMLSocket object using the constructor method, the script installs the `onConnect` method using the assignment operator:

    socket = new XMLSocket()
    socket.onConnect = myOnConnect

Finally, the connection is initiated. If `connect` returns `false`, the movie is sent directly to the frame labeled `connectionFailed`, and `onConnect` is never invoked. If `connect` returns `true`, the movie jumps to a frame labeled `waitForConnection`, which is the "Please wait" screen. The movie 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

`function`, `XMLSocket.connect()`