application.onConnect = function (clientObj [, p1, ..., pN]){}


Invoked when NetConnection.connect() is called from the client. This handler is passed a Client object representing the connecting client. Use the Client object to perform actions on the client in the handler. For example, use this function to accept, reject, or redirect a client connection, perform authentication, define methods on the Client object to be called remotely from, and set the Client.readAccess and Client.writeAccess properties to determine client access rights to server-side objects.

When performing authentication, all of the information required for authentication should be sent from the NetConnection.connect() method to the onConnect() handler as parameters (p1..., pN).

If you don't define an onConnect() handler, connections are accepted by default.

If there are several simultaneous connection requests for an application, the server serializes the requests so that only one application.onConnect() handler is executed at a time. It's a good idea to write code for the application.onConnect() function that is executed quickly to prevent a long connection time for clients.

Note: When you are using the version 2 component framework (that is, when you are loading the components.asc file in your server-side script file), you must use the application.onConnectAccept() method to accept client connections.


Flash Communication Server 1


clientObj A Client object. This object contains information about the client that is connecting to the application.

p1 ..., pN Optional parameters passed to the application.onConnect() handler from the client-side NetConnection.connect() method when a client connects to the application.


A boolean value; true causes the server to accept the connection; false causes the server to reject the connection.

When true is returned, NetConnection.onStatus() is invoked on the client with info.code set to "NetConnection.Connect.Success". When false is returned, NetConnection.onStatus() is invoked on the client with info.code set to "NetConnection.Connect.Rejected".

If null or no value is returned, the server puts the client in a pending state and the client can't receive or send messages. If the client is put in a pending state, you must call application.acceptConnection() or application.rejectConnection() at a later time to accept or reject the connection. For example, you can perform external authentication by making a NetConnection call in your application.onConnect() event handler to an application server and having the reply handler call application.acceptConnection() or application.rejectConnection(), depending on the information received by the reply handler.

You can also call application.acceptConnection() or application.rejectConnection() in the application.onConnect() event handler. If you do, any value returned by the function is ignored.

Note: Returning 1 or 0 is not the same as returning true or false. The values 1 and 0 are treated the same as any other integers and do not accept or reject a connection.

How to use application.onConnect() to accept, reject, or put a client in a pending state.
A. Client-side ActionScript B. Server-Side ActionScript


The following examples show three ways to accept or reject a connection in the onConnect() handler:

(Usage 1)
application.onConnect = function (clientObj [, p1, ..., pN]){
    // Insert code here to call methods that do authentication.
    // Returning null puts the client in a pending state.
    return null;
(Usage 2)
application.onConnect = function (clientObj [, p1, ..., pN]){
    // Insert code here to call methods that do authentication.
    // The following code accepts the connection:
(Usage 3)
application.onConnect = function (clientObj [, p1, ..., pN])
    // Insert code here to call methods that do authentication.
    // The following code accepts the connection by returning true:
    return true;

The following example verifies that the user has sent the password "XXXX". If the password is sent, the user's access rights are modified and the user can complete the connection. In this case, the user can create or write to streams and shared objects in the user's own directory and can read or view any shared object or stream in this application instance.

// This code should be placed in the global scope.

application.onConnect = function (newClient, userName, password){
    // Do all the application-specific connect logic.
    if (password == "XXXX"){
        newClient.writeAccess = "/" + userName;
    } else {
        var err = new Object();
        err.message = "Invalid password";
        this.rejectConnection(newClient, err);

If the password is incorrect, the user is rejected and an information object with a message property set to "Invalid password" is returned to the client side. The object is assigned to infoObject.application. To access the message property, use the following code on the client side:

ClientCom.onStatus = function (info.application.message){
    // Prints "Invalid password"
    // in the Output panel on the client side.