Event
About error handling
As you build video applications, it is important to learn the art of managing connections and streams. In a networked environment, a connection attempt might fail for any of these reasons:
Any section of the network between client and server might be down.
The URI to which the client attempts to connect is incorrect.
The application instance does not exist on the server.
The server is down or busy.
The maximum number of clients or maximum bandwidth threshold may have been exceeded.
If a connection is established successfully, you can then create a NetStream object and stream video. However, the stream might encounter problems. You might need to monitor the current frame rate, watch for buffer empty messages, downsample video and seek to the point of failure, or handle a stream that is not found. Inform customers of errors that occur during playback:
The network connection fails during playback.
The buffer empties before playback is complete.
To be resilient, your application needs to listen for and handle netStatus events that affect connections and streams. As you test and run your application, you can also use the Administration Console to troubleshoot various connection and stream events.
Handle a failed connection
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:
|
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. |
Use the SimpleConnectManage sample, SimpleConnectManage.as, written in ActionScript 3.0.
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; ... } }
Handle a stream not found
If a stream your application attempts to play is not found, a netStatus event is triggered with a code of NetStream.Play.StreamNotFound. Your netStatus event handler should detect this code and take some action, such as displaying a message for the user or playing a standard stream in a default location.
Write the client code
-
In your netStatus event handler, check for the StreamNotFound code and take some action:
private function onNetStatus(event:NetStatusEvent):void { switch (event.info.code) { case "NetStream.Play.StreamNotFound": trace("The server could not find the stream you specified"); ns.play( "public/welcome"); break; ... }