Flash
  Home arrow Flash arrow Page 3 - Video and the Flash Communication Server
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
FLASH

Video and the Flash Communication Server
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 10
    2006-12-21

    Table of Contents:
  • Video and the Flash Communication Server
  • Building the user interface
  • Setting up the NetConnection and showing its status
  • Making the connection

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    More Flash Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "Programming Flash Communication Server,"...
     

    Buy this book now. 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.

    FLASH ARTICLES

    - Critical Flash Vulnerability Heats Up the Web
    - More on Nonpersistent Client-Side Remote Sha...
    - Nonpersistent Client-Side Remote Shared Obje...
    - Using the Decorator Pattern for a Real Web S...
    - Using Concrete Decorator Classes
    - Delving More Deeply into the Decorator Patte...
    - The Decorator Pattern in Action
    - A Simple Decorator Pattern Example
    - Decorator Pattern
    - Organizing Frames and Layers for Flash Anima...
    - Organizing Frames and Layers
    - Using XML and ActionScript with Flex Applica...
    - Interfaces and Events with ActionScript and ...
    - Manipulating Data with ActionScript in Flex ...
    - ActionScript Syntax for Flex Applications







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek