If a connection cannot be made, handle the netStatus event before you create a NetStream object or any other objects. You may need to retry connecting to the server's URI, ask the user to reenter a user name or password, or take some other action.
The event codes to watch for and sample actions to take are as follows:
|
Event |
Action |
|---|---|
|
NetConnection.Connect.Failed |
Display a message for the user that the server is down. |
|
NetConnection.Connect.Rejected |
Try to connect again. |
|
NetConnection.Connect.AppShutDown |
Disconnect all stream objects and close the connection. |
Write client code to handle netStatus events
Create a NetConnection object and connect to the server. Then, write a netStatus event handler in which you detect each event and handle it appropriately for your application, for example:
public function netStatusHandler(event:NetStatusEvent):void
{
trace("connected is: " + nc.connected );
trace("event.info.level: " + event.info.level);
trace("event.info.code: " + event.info.code);
switch (event.info.code)
{
...
case "NetConnection.Connect.Rejected":
trace ("Oops! the connection was rejected");
// try to connect again
break;
case "NetConnection.Connect.Failed":
trace("The server may be down or unreachable");
break;
case "NetConnection.Connect.AppShutDown":
trace("The application is shutting down");
// this method disconnects all stream objects
nc.close();
break;
...
}
}