This is the Title of the Book, eMatter Edition
Copyright © 2007 O’Reilly & Associates, Inc. All rights reserved.
A More Realistic Example
|
137
The Client class cannot be subclassed because the Client constructor is never called
directly in a script. Whenever it is necessary to significantly extend the Client class,
you should use composition. Adding the
user object to each client and then delegat-
ing work, such as getting the client’s connection information, to the
user object is
one example of composition. Other objects can also be added in order to take on
other responsibilities on behalf of the client. For detailed discussions on attaching
methods to a class’s prototype, see ActionScript for Flash MX: The Definitive Guide.
For thorough discussions of composition versus inheritance, see Essential Action-
Script 2.0.
Limiting the Number of Client Connections
The number of currently accepted client connections is always available via the
length property of the application.clients array. Restricting the number of clients
that can connect at any one time is a simple matter of checking the
length property
and rejecting clients when it reaches a certain size. In some applications, it may be
necessary to allow unlimited connections from users of one type and restrict connec-
tions of another type. In Example 4-3, guest connections are not accepted when the
number of accepted clients reaches a certain point:
if (application.clients.length >= MAXCONNECTIONS && user.role == "guest") {
application.rejectConnection(client, {msg: "Chat is already full."});
return;
}
This example shows how to reject a connection to prevent it from being established.
You can use application.disconnect( ) at any time to disconnect a client that has
already been accepted.
Performing Periodic Updates with setInterval( )
You’ll often want to perform some action periodically. On the client side, this may
be achieved in several ways, such as using timelines or enterFrame events. However,
neither of these is available on the server. In server-side code, you can use setInter-
val( ) to call a function or method after a certain amount of time. The function or
method can be called once or many times at regular intervals. In the onAppStart( )
method in Example 4-3, setInterval( ), is called and passed a reference to the listCur-
rentUsers( ) function:
setInterval(listCurrentUsers, 60000);
In this example, listCurrentUsers( ) is called once every minute (60,000 milliseconds).
In turn, listCurrentUsers( ) iterates over the
application.clients array and calls each
client’s user.getConnectionInfo( ) method. This method uses the properties of the
client object, its user object, and User class methods to output text about the client.
See Macromedia’s Server-Side Communication ActionScript Dictionary (available in

Get Programming Flash Communication Server now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.