Video and the Flash Communication Server - Setting up the NetConnection and showing its status
(Page 3 of 4 )
Example 1-2 shows how to create the NetConnection object and add methods to it dynamically. This client-side script, like the code in the following examples, should be placed in the first frame on a separate Scripts layer in the movie’s timeline. Have a look at the sample helloVideo.fla file from the book’s web site and you’ll find the code from Examples 1-2, 1-3, and 1-4 attached to the first frame of the Scripts layer.
Example 1-2 shows how to create the object and add methods to it dynamically. This client-side script, like the code in the following examples, should be placed in the first frame on a separate layer in the movie’s timeline. Have a look at the sample file from the book’s web site and you’ll find the code from Examples 1-2, 1-3, and 1-4 attached to the first frame of the layer.
After thenc variable is assigned a new NetConnection, the code adds two methods. The setID( ) method is invoked by the server to notify the client of its unique user ID. The onStatus( ) method is called whenever a change in connection status occurs. Have a look at Example 1-2 and see if you can figure out what the two methods do. I’ll discuss it in more detail later.
Example 1-2. Setting up the NetConnection
myID = "";
nc = new NetConnection();
/* setID( ) is a remote method that will be called by the server
* after the client connects. Once we get our unique ID from the
* server we can use it to publish our stream.
*/
nc.setID = function (id) {
myID = id;
statusInput.text = "Online. Your client's ID is: " + myID;
ns = new NetStream(nc);
ns.attachAudio(Microphone.get());
var cam = Camera.get();
ns.attachVideo(cam);
ns.publish(id);
_root[id].video.attachVideo(cam);
initUsers();
};
// onStatus( ) is called whenever the status of the network
// connection changes. It is how we know whether we are connected.
nc.onStatus = function (info) {
connectButton.label = "Connect";
connectButton.enabled = true;
switch (info.code) {
case "NetConnection.Connect.Success":
connectButton.label = "Disconnect";
statusInput.text = "Online";
break;
case "NetConnection.Connect.Failed":
statusInput.text = "Cannot reach server. Possible network error.";
break;
case "NetConnection.Connect.Rejected":
statusInput.text = info.application.msg;
break;
case "NetConnection.Connect.Closed":
statusInput.text += " Connection closed.";
break;
}
};
Some time after a connection to the server is made, the setID( ) method is called by the server and itsidparameter is passed as one of four strings: “guest_1”, “guest_2”, “guest_3”, or “guest_4”. The setID( ) method saves the name in themyIDvariable and displays it for the user in the TextInput fieldstatusInput. Then it uses its ID string to publish a stream containing audio and video by creating a NetStream object on thencnet connection. It attaches a microphone and camera object to the stream and publishes it using theidas the stream name. Finally, it displays the stream it is sending in one of the GuestVideo movie clips on the Stage. Since each clip is named after the user IDs the server provides, there will be one clip on the Stage with the same name as the user who has just been assigned. The_rootproperty, which holds a reference to the movie’s main timeline, is used to get a reference to the movie clip._root[id]returns a reference to one of the GuestVideo clips. The statement_root[id].videoreturns a reference to the embedded Video object in the clip, and_root[id].video.attachVideo(cam)displays what the camera sees in the Video object. The setID( ) method also calls the initUsers( ) method to set up the users’ shared object. We’ll have a look at initUsers( ) later.
The onStatus( ) method receives an information object that contains information about the most recent connection-related event. Thecodeproperty of the object is a string in dot-delimited format, such as “NetConnection.Connect.Success”. Depending on the string in thecodeproperty, a message indicating the network connection status is displayed in thestatusInputtext field. As we’ll see shortly, in order to connect to the server, the user must click the Connect button (connectButton). At that time, the button is disabled so the user can’t attempt a second connection while waiting for the result of the first attempt. The onStatus( ) method therefore reenables the button and sets its label to Connect unless the connection has been established.
Next: Making the connection >>
More Flash Articles
More By O'Reilly Media
|
This article is excerpted from chapter one of the book Programming Flash Communication Server, written by Brian Lesser, Giacomo Guilizzoni, Robert Reinhardt, Joey Lott, and Justin Watkins (O'Reilly, 2005; ISBN: 0596005040). Check it out today at your favorite bookstore. Buy this book now.
|
|