<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
	>

<channel>
	<title>overFlash</title>
	<atom:link href="http://overflash.wordpress.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://overflash.wordpress.com</link>
	<description>Just another WordPress.com weblog</description>
	<lastBuildDate>Thu, 24 Jan 2008 15:47:58 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
<cloud domain='overflash.wordpress.com' port='80' path='/?rsscloud=notify' registerProcedure='' protocol='http-post' />
<image>
		<url>http://s2.wp.com/i/buttonw-com.png</url>
		<title>overFlash</title>
		<link>http://overflash.wordpress.com</link>
	</image>
	<atom:link rel="search" type="application/opensearchdescription+xml" href="http://overflash.wordpress.com/osd.xml" title="overFlash" />
	<atom:link rel='hub' href='http://overflash.wordpress.com/?pushpress=hub'/>
		<item>
		<title>Using Flash to create a on-line Chat</title>
		<link>http://overflash.wordpress.com/2008/01/24/using-flash-to-create-a-on-line-chat/</link>
		<comments>http://overflash.wordpress.com/2008/01/24/using-flash-to-create-a-on-line-chat/#comments</comments>
		<pubDate>Thu, 24 Jan 2008 15:35:55 +0000</pubDate>
		<dc:creator>jpgdesign</dc:creator>
				<category><![CDATA[Socket Programming]]></category>
		<category><![CDATA[ActionScript 3]]></category>

		<guid isPermaLink="false">http://overflash.wordpress.com/2008/01/24/using-flash-to-create-a-on-line-chat/</guid>
		<description><![CDATA[To create a flash on-line chat you may want to start here. Socket connections allow the Flash Player to send and load data from a server over a specified network port. The main difference between socket and server connections is that socket connections don&#8217;t automatically close after data transfer is complete. When a socket connection [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=overflash.wordpress.com&amp;blog=2601151&amp;post=5&amp;subd=overflash&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<h1><b>To create a flash on-line chat you may want to start here.<br />
</b></h1>
<p class="docText">Socket <a title="ID-ID-ID-d8129e0-944380" name="ID-ID-ID-d8129e0-944380"></a>connections allow  the Flash Player to send and load data from a server over a specified network  port. The main difference between socket and server connections<a href="http://overflash.wordpress.com/wp-admin/ID-I_0596526954_CHP_20.html#ID-I_0596526954_CHP_20" class="docLink"></a> is that socket connections don&#8217;t automatically close after data transfer  is complete.</p>
<p class="docText">When a socket connection is made, the connection stays open  until the client (the Flash Player) or the server explicitly closes it. Because  of this, sockets enable a special type of communication called data push, which  means that the server sends information to the Flash Player at any time without  a request coming from the Player itself.</p>
<p class="docText">Socket connections are typically used to create multiuser  applications. An example of one such application would be an online chat room.  The chat program might consist of a central chat server with various connected  Flash Player clients. Each time a client <i>.swf </i>sends a message to the  server, the server determines which client should receive the message and pushes  the message to that specific client over the open connection. In this case, the  receiving client didn&#8217;t ask for the message, but rather the message was simply  pushed out to it by the server. When a client closes a connection, the server  notifies the other clients that someone has logged off of the system.</p>
<p class="docText">Two types of socket connections can be made from the Flash  Player to a socket server. They are very similar in behavior and operation, but  have a few subtle differences. The first is an XML socket connection that is  similar to the <i><a title="ID-ID-ID-d8132e0-944383" name="ID-ID-ID-d8132e0-944383"></a>XMLSocket</i> in previous  versions of the Flash Player. New for Flash Player 9 is a binary socket  connection.</p>
<p class="docText">To create an XML socket connection, use the <i>f<a title="ID-ID-ID-d8135e0-944384" name="ID-ID-ID-d8135e0-944384"></a>lash.net.XMLSocket</i> class. To create a  binary socket connection, use the <i>flash.net.Socket</i> class.</p>
<p class="docText">XML socket connections are focused around text. The client and  server communicate by exchanging XML packets containing data. Actions are  carried out by analyzing the contents of the XML packets.</p>
<p class="docText">Binary socket connections are new in ActionScript 3.0 and  enable raw connections that allow for transfer of binary information. Binary  sockets are slightly more advanced than XML sockets because they require a  low-level knowledge of binary datatypes, but they are also more powerful because  you can connect to a wider range of socket servers and generally do more with  them. For example, binary sockets allow you to connect to mail servers (via <a title="ID-ID-ID-d8138e0-944385" name="ID-ID-ID-d8138e0-944385"></a>POP3, <a title="ID-ID-ID-d8141e0-944386" name="ID-ID-ID-d8141e0-944386"></a>SMTP,  and <a title="ID-ID-ID-d8144e0-944387" name="ID-ID-ID-d8144e0-944387"></a>IMAP), news servers (via <a title="ID-ID-ID-d8147e0-944388" name="ID-ID-ID-d8147e0-944388"></a>NNTP), chat servers, or even implement screen  sharing and remote desktop applications by connection to a VNC server (via  RFB).</p>
<p class="docText">Regardless of which socket connection type you use, the  connection behaves in an asynchronous manner. This means that you can&#8217;t read  data from the socket connection until you have been notified that data is  available via an event handler. All data reading must be done in an event  handler that executes automatically when data is available. Synchronous socket  connections, when attempting to read data from the socket causes your program to  wait until data becomes available in the socket, are easier to program but not  as efficient. You&#8217;ll learn more about how to deal with the asynchronous nature  of the Flash Players socket connections as you go through this chapter.</p>
<h3 class="docSection1Title">Connecting to a Socket  Server</h3>
<p><a title="ID-ID-ID-d8107e500-Problem635" name="ID-ID-ID-d8107e500-Problem635"></a></p>
<h4 class="docSection2Title">Problem</h4>
<p class="docText">You<a title="ID-ID-ID-d8150e0-944389" name="ID-ID-ID-d8150e0-944389"></a> want to establish a  connection with a socket server.</p>
<p><a title="ID-ID-ID-d8107e537-Solution639" name="ID-ID-ID-d8107e537-Solution639"></a></p>
<h4 class="docSection2Title">Solution</h4>
<p class="docText">Use either the <i><a title="ID-ID-ID-d8153e0-944391" name="ID-ID-ID-d8153e0-944391"></a>Socket.connect( )</i> or <i><a title="ID-ID-ID-d8156e0-944392" name="ID-ID-ID-d8156e0-944392"></a>XMLSocket.connect( )</i> method and listen for  the <tt>connect</tt> event to be notified when a connection is made.</p>
<p><a title="ID-ID-ID-d8107e616-Discussion643" name="ID-ID-ID-d8107e616-Discussion643"></a></p>
<h4 class="docSection2Title">Discussion</h4>
<p class="docText">To connect to a socket server, there are two critical pieces of  information that you need to know before attempting to make a connection. The  first is the domain name or IP address of the server to which the connection  will be made, and the second is the port over which the connection should take  place.</p>
<p class="docText">Whether you are using a <i>Socket</i> or an <i>XMLSocket</i>  instance, the connection process is exactly the same; both classes define a  <i><a title="ID-ID-ID-d8159e0-944393" name="ID-ID-ID-d8159e0-944393"></a>connect( )</i> method that takes two  parameters:</p>
<p><a title="ID-ID-ID-d8162e0-944394" name="ID-ID-ID-d8162e0-944394"></a><a title="ID-ID-ID-d8165e0-944395" name="ID-ID-ID-d8165e0-944395"></a></p>
<dl>
<dt> <span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont"><a title="ID-ID-ID-d8162e0-944394" name="ID-ID-ID-d8162e0-944394"></a>host</span></span> </span> </dt>
<dd>
<p class="docList">A string value specifying the host to connect to, either with a  domain name such as <tt>www.example.com</tt>, or with an IP address such as  <tt>192.168.1.101</tt>. To connect to the web server the Flash movie is being  served from, pass <tt>null</tt> instead of a string hostname.</p>
</dd>
<dt> <span class="docPubcolor"><span class="docPubcolor"><span class="docMonofont"><a title="ID-ID-ID-d8165e0-944395" name="ID-ID-ID-d8165e0-944395"></a>port</span></span> </span> </dt>
<dd>
<p class="docList">An <i>int</i> value specifying the port number that should be  used to connect to the host. The port value must be at least 1024, unless the  server has a policy file specifically allowing ports less than  1024.</p>
</dd>
</dl>
<p class="docText">Because of the asynchronous nature of socket programming in  Flash, the <i>connect( )</i> method does not wait for a connection to happen  before continuing to the next line of ActionScript code. If you try to interact  with a socket before a connection has been fully established, you&#8217;ll encounter  unexpected results and your code won&#8217;t work correctly.</p>
<p class="docText">The proper way to connect to a socket is to first add an event  listener for the <tt>connect</tt> event before attempting to call <i>connect(  )</i>. The <tt>connect</tt> event is dispatched by both <i><a title="ID-ID-ID-d8168e0-944397" name="ID-ID-ID-d8168e0-944397"></a><a title="ID-ID-ID-d8168e5-944397" name="ID-ID-ID-d8168e5-944397"></a>Socket</i>  and <i>XMLSocket</i> objects when a successful connection has been made, letting  you know that the socket is ready to be interacted with.</p>
<p class="docText">The code for connecting a <i>Socket</i> instance to a socket  server running on <tt>localhost</tt> over port 2900 looks like this:</p>
<pre>package {
  import flash.display.Sprite;
  import flash.events.*;
  import flash.net.Socket;

  public class SocketExample extends Sprite {

    private var socket:Socket;

    public function SocketExample(  ) {
      socket = new Socket(  );

      // Add an event listener to be notified when the connection
      // is made
      socket.addEventListener( Event.CONNECT, onConnect );

      // Connect to the server
      socket.connect( "localhost", 2900 );
    }

    private function onConnect( event:Event ):void {
      trace( "The socket is now connected..." );
    }

  }
}</pre>
<p class="docText">If you want to connect via <i>XMLSocket</i> instead, the code  is almost exactly the same. First, you create the event listener for the  <tt>connect</tt> event, and then you invoke the <i>connect( )</i> method. The  only difference is that all <i>Socket</i> references should be replaced with  <i>XMLSocket</i>:</p>
<pre>package {
  import flash.display.Sprite;
  import flash.events.*;
  import flash.net.XMLSocket;

  public class SocketExample extends Sprite {

    private var socket:XMLSocket;

    public function SocketExample(  ) {
      socket = new XMLSocket(  );

      // Add an event listener to be notified when the connection is made
      socket.addEventListener( Event.CONNECT, onConnect );

      // Connect to the server
      socket.connect( "localhost", 2900 );
    }

    private function onConnect( event:Event ):void {
      trace( "The xml socket is now connected..." );
    }

  }
}</pre>
<p class="docText">If the connection fails, one of two things can happen: either  the connection fails right away and a runtime error is generated, or an  <tt>ioError</tt> or <tt>securityError</tt> event is raised to indicate that the  connection could not be completed successfully.</p>
<p class="docText">&nbsp;</p>
<p class="docText">Remember, when connecting to a host with a socket connection,  the following Flash Player security sandbox rules apply:</p>
<div style="font-weight:bold;">
<ol class="docList">
<li>
<div style="font-weight:normal;">
<p class="docList">The <i>.swf</i> and host must be in the exact same domain in  order for a successful connection to be made.</p>
</div>
</li>
<li>
<div style="font-weight:normal;">
<p class="docList">A <i>.swf </i>delivered over a network cannot connect to a  local server.</p>
</div>
</li>
<li>
<div style="font-weight:normal;">
<p class="docList">A local untrusted <i>.swf</i> cannot access any network  resources.</p>
</div>
</li>
<li>
<div style="font-weight:normal;">
<p class="docList">To allow cross-domain access or connections to a port lower  than 1024, a cross-domain policy file can be used.</p>
</div>
</li>
</ol>
</div>
<p class="docText">Violating the security sandbox by attempting to connect to an  untrusted domain or on a low port raises a <tt>securityError</tt> event. These  issues can both be worked around by using a cross-domain policy file<a href="http://overflash.wordpress.com/wp-admin/ID-I_0596526954_CHP_3_SECT_13.html#ID-I_0596526954_CHP_3_SECT_13" class="docLink"></a>. To use a cross-domain policy file with either a <i>Socket </i>or  <i>XMLSocket </i>object, you need to load the policy file by using  <i>flash.system.Security.loadPolicyFile( )</i>, as shown here:</p>
<pre>Security.loadPolicyFile("http://www.rightactionscript.com/crossdomain.xml");</pre>
<p class="docText">When assembling the cross-domain policy file, you should  specify not only the allowed domains, but also the allowed ports. If you do not  specify allowed ports, Flash Player assumes that port 80 (standard HTTP port) is  the only allowed port. You can specify a comma-delimited list using the  <tt>port</tt> attribute of the <tt>&lt;allow-access-from&gt;</tt> tag. The  following policy file allows all domains to connect to ports 80 and 110  (standard HTTP and POP mail ports):</p>
<pre>&lt;?xml version="1.0"?&gt;

&lt;!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"&gt;
&lt;cross-domain-policy&gt;
   &lt;allow-access-from domain="*" to-ports="80,110" /&gt;
&lt;/cross-domain-policy&gt;</pre>
<p class="docText">&nbsp;</p>
<p class="docText">&nbsp;</p>
<h3 class="docSection1Title">Sending Data</h3>
<p><a title="ID-ID-ID-d8107e1923-Problem803" name="ID-ID-ID-d8107e1923-Problem803"></a></p>
<h4 class="docSection2Title">Problem</h4>
<p class="docText">You <a title="ID-ID-ID-d8177e0-944398" name="ID-ID-ID-d8177e0-944398"></a>want to send data to a  socket server.</p>
<p><a title="ID-ID-ID-d8107e1960-Solution807" name="ID-ID-ID-d8107e1960-Solution807"></a></p>
<h4 class="docSection2Title">Solution</h4>
<p class="docText">For <i>Socket</i> objects, use the write methods (<i><a title="ID-ID-ID-d8180e0-944400" name="ID-ID-ID-d8180e0-944400"></a>writeByte( )</i>, <i><a title="ID-ID-ID-d8183e0-944401" name="ID-ID-ID-d8183e0-944401"></a>writeUTFBytes( )</i>, etc.) to write the data  to the buffer and call <i><a title="ID-ID-ID-d8186e0-944402" name="ID-ID-ID-d8186e0-944402"></a>flush( ) </i>to  send the data. For <i>XMLSocket </i>objects, use the <i>send( )  </i>method.</p>
<p><a title="ID-ID-ID-d8107e2084-Discussion811" name="ID-ID-ID-d8107e2084-Discussion811"></a></p>
<h4 class="docSection2Title">Discussion</h4>
<p class="docText">The <i><a title="ID-ID-ID-d8189e0-944403" name="ID-ID-ID-d8189e0-944403"></a><a title="ID-ID-ID-d8189e5-944403" name="ID-ID-ID-d8189e5-944403"></a>Socket </i>and <i>XMLSocket </i>classes define  different APIs for sending data to the socket server. Let&#8217;s look at the  <i>Socket </i>API first.</p>
<p class="docText">When you want to send data to a socket server using a <i>Socket  </i>object, you first must write the data to the buffer. The <i>Socket </i>class  defines a slew of methods for writing data. Each of the methods writes a  different type of data (or writes the data differently). The methods are <i><a title="ID-ID-ID-d8192e0-944404" name="ID-ID-ID-d8192e0-944404"></a>writeBoolean( )</i>, <i>writeByte( )</i>,  <i>writeBytes( )</i>, <i><a title="ID-ID-ID-d8195e0-944406" name="ID-ID-ID-d8195e0-944406"></a>writeDouble( )</i>,  <i><a title="ID-ID-ID-d8198e0-944407" name="ID-ID-ID-d8198e0-944407"></a>writeFloat( )</i>, <i><a title="ID-ID-ID-d8201e0-944408" name="ID-ID-ID-d8201e0-944408"></a>writeInt( )</i>, <i><a title="ID-ID-ID-d8204e0-944409" name="ID-ID-ID-d8204e0-944409"></a>writeMultiByte( )</i>, <i><a title="ID-ID-ID-d8207e0-944410" name="ID-ID-ID-d8207e0-944410"></a>writeObject( )</i>, <i><a title="ID-ID-ID-d8210e0-944411" name="ID-ID-ID-d8210e0-944411"></a>writeShort( )</i>, <i><a title="ID-ID-ID-d8213e0-944412" name="ID-ID-ID-d8213e0-944412"></a>write- UnsignedInt( )</i>, <i><a title="ID-ID-ID-d8216e0-944413" name="ID-ID-ID-d8216e0-944413"></a>writeUTF( )</i>, and <i><a title="ID-ID-ID-d8219e0-944414" name="ID-ID-ID-d8219e0-944414"></a>writeUTFBytes( )</i>. Most of the methods  accept one parameter of the type implied by the name of the method. For example,  <i>writeBoolean( ) </i>accepts a Boolean parameter and <i>writeByte( )</i>,  <i>writeDouble( )</i>, <i>writeFloat( )</i>, <i>writeInt( )</i>, <i>writeShort(  )</i>, and <i>writeUnsignedInt( ) </i>accept numeric parameters. The  <i>writeObject( ) </i>method accepts an object parameter that must be  serializable to AMF format. The <i>writeBytes( ) </i>method allows you to pass  it a <i>ByteArray </i>parameter along with <i>offset </i>and <i>length  </i>parameters. For example, the following calls <i>writeBytes( ) </i>passing,  it a reference to a <i>ByteArray </i>object and specifying that it should write  all the bytes (starting at offset 0 with length equal to the length of the  <i>ByteArray</i>):</p>
<pre>socket.writeBytes(byteArray, 0, byteArray.length);</pre>
<p class="docText">The <i>writeUTF( ) </i>and <i>writeUTFBytes( ) </i>methods  allow you to write strings. Each method accepts a <i>string </i>parameter. The  <i>writeUTFBytes( ) </i>method simply writes the string as bytes. The  <i>writeUTF( ) </i>method first writes the number of bytes before writing the  actual byte data.</p>
<p class="docText">The <i>writeMultiByte( ) </i>method also writes string data,  but using a nondefault character set. The method requires two parameters: the  string to write and the name of the character set to use. The help documentation  for Flash and Flex list the supported character sets along with the labels and  aliases for each. Use the label value as a string when specifying the character  set for <i>writeMultiByte( )</i>. The following example writes a string  <tt>example</tt> using Unicode:</p>
<pre>socket.writeMultiByte("example", "unicode");</pre>
<p class="docText">Which method or methods you use to write data to a <i>Socket  </i>object is entirely dependent on what sort of data you want to write and what  sort of data the server expects. Using a <i>Socket </i>object, you can write a  Telnet or POP mail client entirely by using ActionScript. Both protocols expect  ASCII text commands. For example, after connecting to a POP server, you can  specify a user with the <tt>USER</tt> command. The following writes such a  command to a <i>Socket </i>object:</p>
<pre>// POP servers expect a newline (\n) to execute the preceding command.
socket.writeUTFBytes("USER exampleUsername\n");</pre>
<p class="docText">Writing the data to the <i>Socket </i>object does not actually  send the data to the socket server. Each call to a <i>write </i>method appends  the data to the <i>Socket </i>object. For example, the following writes four  bytes to a <i>Socket </i>object, but none of them are sent:</p>
<pre>socket.writeByte(1);
socket.writeByte(5);
socket.writeByte(4);
socket.writeByte(8);</pre>
<p class="docText">When you want to send the accumulated data to the socket  server, use the <i>flush( ) </i>method. The <i>flush( ) </i>method simply sends  all the written data and clears the buffer:</p>
<pre>socket.flush(  );</pre>
<p class="docText">The <i>XMLSocket </i>class has a much simpler API for sending  data. Writing and sending data occur with one method aptly named <i>send( )</i>.  The <i>send( ) </i>method accepts parameter of any datatype. It converts the  parameter to a string and sends it to the server. Traditionally the parameter is  an XML object or a string containing data structured as XML:</p>
<pre>xmlSocket.send(xml);</pre>
<p class="docText">However, the exact format of the data is entirely dependent on  the format the server expects. If the server expects XML-formatted data, then  you&#8217;ll need to send XML-formatted data. If the server expects URL-encoded data,  then you&#8217;ll need to send URL-encoded data.</p>
<p class="docText">&nbsp;</p>
<p class="docText">&nbsp;</p>
<h3 class="docSection1Title">Receiving Data</h3>
<p><a title="ID-ID-ID-d8107e3055-Problem857" name="ID-ID-ID-d8107e3055-Problem857"></a></p>
<h4 class="docSection2Title">Problem</h4>
<p class="docText">You<a title="ID-ID-ID-d8225e0-944415" name="ID-ID-ID-d8225e0-944415"></a> want to read data from  a socket server.</p>
<p><a title="ID-ID-ID-d8107e3092-Solution861" name="ID-ID-ID-d8107e3092-Solution861"></a></p>
<h4 class="docSection2Title">Solution</h4>
<p class="docText">For <i>Socket</i> instances, subscribe to the  <tt>socketData</tt> event and invoke one of the <i>read </i>methods, such as  <i><a title="ID-ID-ID-d8228e0-944416" name="ID-ID-ID-d8228e0-944416"></a>readByte( )</i> or <i><a title="ID-ID-ID-d8231e0-944417" name="ID-ID-ID-d8231e0-944417"></a>readInt( )</i>, in the event handler, making  sure not to read past <tt>bytesAvailable</tt>.</p>
<p class="docText">For <i><a title="ID-ID-ID-d8234e0-944418" name="ID-ID-ID-d8234e0-944418"></a>XMLSocket</i>  instances, subscribe to the <tt>data</tt> event and interpret the XML data  received inside of the event handler.</p>
<p><a title="ID-ID-ID-d8107e3224-Discussion867" name="ID-ID-ID-d8107e3224-Discussion867"></a></p>
<h4 class="docSection2Title">Discussion</h4>
<p class="docText">Receiving data from a socket connection depends on the type of  socket you use. Both <i><a title="ID-ID-ID-d8237e0-944419" name="ID-ID-ID-d8237e0-944419"></a>Socket</i> and  <i>XMLSocket</i> are capable of receiving data from a server, but they do so  using slightly different techniques. Let&#8217;s focus on how the <i>Socket</i> class  works first before discussing <i>XMLSocket</i>.</p>
<p class="docText">As you&#8217;ve learned in the introduction to this chapter, sockets  in Flash behave asynchronously. Therefore, it&#8217;s not possible to simply create a  socket connection and attempt to read data from the socket right away. The  <i>read </i>methods don&#8217;t wait for data to be transferred from the server before  returning. Instead, you can only read data from a socket after the client has  already downloaded the data from the host server. It is an error to try and read  data from a <i>Socket</i> before any data is available.</p>
<p class="docText">To know when data is available to be read, the  <tt>socketData</tt> event is broadcasted from <i>Socket</i> instances. By adding  an event listener for the <tt>socketData</tt> event, your event handler is  invoked anytime there is new data received from the socket server. Inside the  event handler is where you write code to read and interpret the received  data.</p>
<p class="docText">To read the data sent from the server, the <i>Socket</i> class  provides a number of different <i>read </i>methods, depending on the type of  data you want to read. For instance, you can read a byte with the <i>readByte(  )</i> method, or read an unsigned integer with the <i>readUnsignedInt( )</i>  method.</p>
<p class="docText">&nbsp;</p>
<p class="docText">&nbsp;</p>
<h3 class="docSection1Title">Handshaking with a Socket Server</h3>
<p><a title="ID-ID-ID-d8107e6116-Problem1098" name="ID-ID-ID-d8107e6116-Problem1098"></a></p>
<h4 class="docSection2Title">Problem</h4>
<p class="docText">You want to do <a title="ID-ID-ID-d8282e0-944434" name="ID-ID-ID-d8282e0-944434"></a><a title="ID-ID-ID-d8282e5-944434" name="ID-ID-ID-d8282e5-944434"></a>handshaking with a socket server and need to  know what the received data&#8217;s context is to know how to process it.</p>
<p><a title="ID-ID-ID-d8107e6153-Solution1102" name="ID-ID-ID-d8107e6153-Solution1102"></a></p>
<h4 class="docSection2Title">Solution</h4>
<p class="docText">Create different constant variables to represent states of the  protocol. Use the constants to map particular processing functions with the  corresponding state. In a <tt>socketData</tt> event handler, call the  appropriate function by invoking it through the state map.</p>
<p><a title="ID-ID-ID-d8107e6181-Discussion1106" name="ID-ID-ID-d8107e6181-Discussion1106"></a></p>
<h4 class="docSection2Title">Discussion</h4>
<p class="docText">A common scenario when connecting to a socket is going through  a handshake process. Typically, the server initially sends data to the client.  The client then responds to the data in a particular manner, and the server  responds again accordingly. This entire process repeats until the handshaking is  complete and a &#8220;normal&#8221; connection is established.</p>
<p class="docText">It gets difficult to process the response from the server  because the <tt><a title="ID-ID-ID-d8285e0-944433" name="ID-ID-ID-d8285e0-944433"></a>socketData</tt> event  handler does not keep track of context. That is, there is no &#8220;why&#8221; sent along  with the server response, or no &#8220;this data is in response to&#8221; processing  directive. Knowing how to process the response from the server is not usually  something that can be gathered through the response itself, especially when the  response varies. Perhaps one response returns two bytes and another returns an  integer followed by a double. You can begin to see how this presents itself as a  problem.</p>
<p class="docText">The solution is to create various state constants to represent  the different contexts in which the server sends data to the client. By  associating each of these constants with a particular function to handle the  data, you can easily call the correct processing function based on the current  state of the protocol.</p>
<p class="docText">Consider the following handshaking scenario that happens when  you connect to a socket server:</p>
<div style="font-weight:bold;">
<ol class="docList">
<li>
<div style="font-weight:normal;">
<p class="docList">The server responds immediately when it connects with an  integer representing the highest version of the protocol that the server  supports.</p>
</div>
</li>
<li>
<div style="font-weight:normal;">
<p class="docList">The client responds with an integer to indicate the actual  version of the protocol that should be used for communication.</p>
</div>
</li>
<li>
<div style="font-weight:normal;">
<p class="docList">The server sends back an 8-byte authentication challenge.</p>
</div>
</li>
<li>
<div style="font-weight:normal;">
<p class="docList">The client sends the authentication challenge back to the  server.</p>
</div>
</li>
<li>
<div style="font-weight:normal;">
<p class="docList">The server closes the connection if the client response was not  what the server was expecting, or, at this point, the protocol moves into a  normal operating mode and the handshaking is complete.</p>
</div>
</li>
</ol>
</div>
<p class="docText">In reality, Step 4 involves a more secure response to an  authentication challenge. Instead of just sending back the challenge verbatim,  you would really want to use some sort of encryption with a user-supplied key.  Perhaps the client asks the user for a password, and then the password entered  is used as the encryption key for the 8-byte challenge. The encrypted challenge  is then sent back to the server. If this challenge response matches what the  server expected, then the client knew the right password and the connection  should be allowed.</p>
<p class="docText">To implement the handshaking processed outlined, you first want  to create constants to represent the different kinds of data returned from the  server. First, there is determining the version from Step 1. Second, there is  receiving the challenge from Step 3. Finally, there is the normal operating mode  from Step 5. These can be represented by the following constants:</p>
<pre>public const DETERMINE_VERSION:int = 0;
public const RECEIVE_CHALLENGE:int = 1;
public const NORMAL:int = 2;</pre>
<p class="docText">It doesn&#8217;t matter what values are given to the constants.  Rather, the only important part is that all the values are different so no two  constants represent the same <i>int</i> value.</p>
<p class="docText">The next step is to create different functions to process the  data. The three functions created will be named <i><a title="ID-ID-ID-d8288e0-944436" name="ID-ID-ID-d8288e0-944436"></a>readVersion( )</i>, <i><a title="ID-ID-ID-d8291e0-944437" name="ID-ID-ID-d8291e0-944437"></a>readChallenge( )</i>, and <i><a title="ID-ID-ID-d8294e0-944438" name="ID-ID-ID-d8294e0-944438"></a>readNormalProtocol( )</i>. After the functions  have been defined, a map must be created to associate one of the previous state  constants with the function used to process the data received during that state.  The code for that looks like this:</p>
<pre>stateMap = new Object(  );
stateMap[ DETERMINE_VERSION ] = readVersion;
stateMap[ RECEIVE_CHALLENGE ] = readChallenge;
stateMap[ NORMAL            ] = readNormalProtocol;</pre>
<p class="docText">The final step is to code the <tt>socketData</tt> event handler  in such a way that the correct processing function is invoked based on the  current state of the protocol. To do this, a <tt>currentState</tt> <i>int</i>  variable is created. Then, using the <tt>stateMap</tt>, the processing function  is invoked by looking up the function associated with <tt>currentState</tt>:</p>
<pre>var processFunc:Function = stateMap[ currentState ];
processFunc(  ); // Invoke the appropriate processing function</pre>
<p class="docText">There is a little bit of bookkeeping involved in this process.  Be sure to update <tt>currentState</tt> in your code to accurately reflect the  current state of the protocol.</p>
<p class="docText">The entire code example to process the handshaking scenario  previously described looks like the following:</p>
<pre>package {
  import flash.display.Sprite;
  import flash.events.ProgressEvent;
  import flash.net.Socket;
  import flash.utils.ByteArray;

  public class SocketExample extends Sprite {

    // The state constants to describe the protocol
    public const DETERMINE_VERSION:int = 0;
    public const RECEIVE_CHALLENGE:int = 1;
    public const NORMAL:int = 2;

    // Maps a state to a processing function
    private var stateMap:Object;

    // Keeps track of the current protocol state
    private var currentState:int;

    private var socket:Socket;

    public function SocketExample(  ) {
      // Initialzes the states map
      stateMap = new Object(  );
      stateMap[ DETERMINE_VERSION ] = readVersion;
      stateMap[ RECEIVE_CHALLENGE ] = readChallenge;
      stateMap[ NORMAL            ] = readNormalProtocol;

      // Initialze the current state
      currentState = DETERMINE_VERSION;

      // Create and connect the socket
      socket = new Socket(  );
      socket.addEventListener( ProgressEvent.SOCKET_DATA, onSocketData );
      socket.connect( "localhost", 2900 );
    }

    private function onSocketData( event:ProgressEvent ):void {
      // Look up the processing function based on the current state
      var processFunc:Function = stateMap[ currentState ];
      processFunc(  );
    }

    private function readVersion(  ):void {
      // Step 1 - read the version from the server
      var version:int = socket.readInt(  );

      // Once the version is read, the next state is receiving
      // the challenge from the server
      currentState = RECEIVE_CHALLENGE;

      // Step 2 - write the version back to the server
      socket.writeInt( version );
      socket.flush(  );
    }

    private function readChallenge(  ):void {
      // Step 3 - read the 8 byte challenge into a byte array
      var bytes:ByteArray = new ByteArray(  );
      socket.readBytes( bytes, 0, 8 );

      // After the challenge is received, the next state is
      // the normal protocol operation
      currentState = NORMAL;

      // Step 4 - write the bytes back to the server
      socket.writeBytes( bytes );
      socket.flush(  );
    }

    private function readNormalProtocol(  ):void {
      // Step 5 - process the normal socket messages here now that
      // that handshaking process is complete
    }
  }
}</pre>
<h3 class="docSection1Title">Disconnecting from a Socket Server</h3>
<p><a title="ID-ID-ID-d8107e7465-Problem1316" name="ID-ID-ID-d8107e7465-Problem1316"></a></p>
<h4 class="docSection2Title">Problem</h4>
<p class="docText">You want<a title="ID-ID-ID-d8297e0-944439" name="ID-ID-ID-d8297e0-944439"></a> to disconnect from  a socket server, or be notified when the server disconnects you.</p>
<p><a title="ID-ID-ID-d8107e7505-Solution1320" name="ID-ID-ID-d8107e7505-Solution1320"></a></p>
<h4 class="docSection2Title">Solution</h4>
<p class="docText">Invoke the <i><a title="ID-ID-ID-d8300e0-944440" name="ID-ID-ID-d8300e0-944440"></a>Socket.close(  )</i> or <i><a title="ID-ID-ID-d8303e0-944441" name="ID-ID-ID-d8303e0-944441"></a>XMLSocket.close( )</i> method <a title="ID-ID-ID-d8306e0-944539" name="ID-ID-ID-d8306e0-944539"></a>to explicitly close the connection, or listen  for the <tt>close</tt> event to be notified when the server closes the  connection for you.</p>
<p><a title="ID-ID-ID-d8107e7610-Discussion1324" name="ID-ID-ID-d8107e7610-Discussion1324"></a></p>
<h4 class="docSection2Title">Discussion</h4>
<p class="docText">A general rule to follow when programming is to clean up after  yourself. That is, if you create an object, you should also delete it when it is  no longer necessary. In this case, whenever you connect to a socket server, you  should explicitly close the connection when you&#8217;re done. Leaving an unused  socket connection open is a waste of resources and should be avoided if at all  possible. If you don&#8217;t close a connection, then the server may continue to keep  an open socket connection that is not being used, which can quickly cause a  server to overrun its allotment of allowed socket connections.</p>
<p class="docText">Closing a socket connection is the same for both <i>Socket</i>  and <i>XMLSocket</i> instances. All you need to do is invoke the <i><a title="ID-ID-ID-d8309e0-944442" name="ID-ID-ID-d8309e0-944442"></a>close( )</i> method on the socket instance:</p>
<pre>// Assume socket is a connected Socket instance
socket.close(  );  // Disconnect from the server</pre>
<p class="docText">Using an <i>XMLSocket </i>is exactly the same:</p>
<pre>// Assume xmlSocket is a connected XMLSocket instance
xmlSocket.close(  );  // Disconnect from the server</pre>
<p class="docText">The <i>close( )</i> method is useful for letting the server  know that the client wants to disconnect. To be notified when the server closes  the connection on its own, you should listen for the <tt>close</tt> event by  calling <i>addEventListener( )</i> on the <i>Socket</i> or <i>XMLSocket</i>  instance with <tt>Event.CLOSE</tt> as the event type; for example:</p>
<pre>var socket:Socket = new Socket(  );

// Add an event listener to be notified when the server disconnects
// the client
socket.addEventListener( Event.CLOSE, onClose );</pre>
<p class="docText">Invoking the <i>close( )</i> method does not raise the  <tt>close</tt> event. Instead, the <tt>close</tt> event is raised only when the  server initiates the disconnection.</p>
<pre>Once a socket is closed, it is no longer capable of reading or writing data. If
you'd like to reuse the socket, you have to establish a connection again.</pre>
<h3 class="docSection1Title">Handling Socket Errors</h3>
<p><a title="ID-ID-ID-d8107e8242-Problem1364" name="ID-ID-ID-d8107e8242-Problem1364"></a></p>
<h4 class="docSection2Title">Problem</h4>
<p class="docText">You <a title="ID-ID-ID-d8312e0-944443" name="ID-ID-ID-d8312e0-944443"></a>want to handle errors  that might occur when using sockets.</p>
<p><a title="ID-ID-ID-d8107e8279-Solution1368" name="ID-ID-ID-d8107e8279-Solution1368"></a></p>
<h4 class="docSection2Title">Solution</h4>
<p class="docText">Use <tt><a title="ID-ID-ID-d8315e0-944444" name="ID-ID-ID-d8315e0-944444"></a><a title="ID-ID-ID-d8315e5-944444" name="ID-ID-ID-d8315e5-944444"></a>try/catch</tt> to handle I/O and end of file  (EOF) errors.</p>
<p><a title="ID-ID-ID-d8107e8319-Discussion1372" name="ID-ID-ID-d8107e8319-Discussion1372"></a></p>
<h4 class="docSection2Title">Discussion</h4>
<p class="docText">Both the <i><a title="ID-ID-ID-d8318e0-944445" name="ID-ID-ID-d8318e0-944445"></a><a title="ID-ID-ID-d8318e5-944445" name="ID-ID-ID-d8318e5-944445"></a>Socket</i> and <i>XMLSocket</i> classes behave  similarly in regard to errors and error events. When calling the <i>connect( )  </i>method, <i>Socket </i>and <i>XMLSocket </i>objects can throw an error of  type <i>SecurityError </i>when either of the following conditions is true:</p>
<ul>
<li>
<p class="docList">The <i>.swf </i>is classified as local untrusted.</p>
</li>
<li>
<p class="docList">The port number is higher than 655535.</p>
</li>
</ul>
<p class="docText">When calling<a title="ID-ID-ID-d8321e0-944446" name="ID-ID-ID-d8321e0-944446"></a> <a title="ID-ID-ID-d8321e5-944446" name="ID-ID-ID-d8321e5-944446"></a><i>send( ) </i>(<i>XMLSocket</i>) or <i>flush(  ) </i>(<i>Socket</i>), the method can throw an error of type <i>IOError</i> if  the socket isn&#8217;t connected. Although you can (and likely should) place the  <i>send( )</i> or <i>flush( ) </i>method calls within try/catch blocks, you  should not rely on try/catch blocks as part of your application logic. Rather,  use an if statement to test whether or not the socket object&#8217;s connected  property is true before calling <i>send( ) </i>of <i>flush( ) </i>if you want  such a test to be part of the application logic. For example, the following uses  an <tt>if</tt> statement as part of the application logic to call a <i><a title="ID-ID-ID-d8324e0-944447" name="ID-ID-ID-d8324e0-944447"></a>connectToSocketServer( ) </i>method if the  <i>Socket </i>object isn&#8217;t currently connected. It also uses a  <tt>TRy</tt>/<tt>catch</tt> block to write to a log if the <i>flush( )  </i>method throws an error:</p>
<p>if ( socket.connected ) {     try {         socket.flush(  );     }     catch( error:IOError ) {         logInstance.write( &#8220;socket.flush error\n&#8221; + error );     } } else {     connectToSocketServer(  ); }</p>
<p class="docText">All of the <i>Socket </i><tt>read</tt> methods can throw errors  of type <i>EOFError </i>and <i>IOError</i>. EOF errors occur when you try to  read data, but nothing is available. I/O errors occur when you try to read from  a socket that is closed.</p>
<p class="docText">In addition to the errors thrown by methods of the <i>Socket  </i>and <i>XMLSocket </i>classes, objects of those classes also dispatch error  events. There are two basic types of error events that occur with  sockets<i>IOError</i> and <i>securityError</i>. The <i>IOError</i> event is of  type <i>IOErrorEvent</i>, and it occurs when data fails to send or load. The  <i>securityError</i> event is of type <i>SecurityErrorEvent</i>, and it occurs  when a socket attempts to connect to a server but fails because the server is  either outside of the sandbox or because the port number is lower than 1024.<a title="ID-ID-ID-d8327e0-944381" name="ID-ID-ID-d8327e0-944381"></a></p>
<p class="docText">Both security error event scenarios are correctable by way of a  cross-domain policy file.</p>
<p>From ActionScript 3.0 Cookbook (The best!)</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/overflash.wordpress.com/5/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/overflash.wordpress.com/5/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/overflash.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/overflash.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/overflash.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/overflash.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/overflash.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/overflash.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/overflash.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/overflash.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/overflash.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/overflash.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/overflash.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/overflash.wordpress.com/5/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/overflash.wordpress.com/5/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/overflash.wordpress.com/5/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=overflash.wordpress.com&amp;blog=2601151&amp;post=5&amp;subd=overflash&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://overflash.wordpress.com/2008/01/24/using-flash-to-create-a-on-line-chat/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5833fa31c9587997e99322f89f90de81?s=96&#38;d=identicon" medium="image">
			<media:title type="html">jpgdesign</media:title>
		</media:content>
	</item>
		<item>
		<title>SWF Players</title>
		<link>http://overflash.wordpress.com/2008/01/24/swf-players/</link>
		<comments>http://overflash.wordpress.com/2008/01/24/swf-players/#comments</comments>
		<pubDate>Thu, 24 Jan 2008 00:53:56 +0000</pubDate>
		<dc:creator>jpgdesign</dc:creator>
				<category><![CDATA[Flash Utils]]></category>

		<guid isPermaLink="false">http://overflash.wordpress.com/2008/01/24/swf-players/</guid>
		<description><![CDATA[If you are not pleased with the performance of the standalone player that comes with Flash, there are many makers of SWF Players. Some of these contain special features like hardware acceleration. FULLSCREEN Special projector tool created to playback movies fullscreen, without the loss of performance. Can switch screen resolution, does hardware acceleration of playback [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=overflash.wordpress.com&amp;blog=2601151&amp;post=4&amp;subd=overflash&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>If you are not pleased with the performance of the standalone player that comes with Flash, there are many makers of SWF Players. Some of these contain special features like hardware acceleration.<br />
<b><br />
FULLSCREEN</b><br />
Special projector tool created to playback movies fullscreen, without the loss of performance. Can switch screen resolution, does hardware acceleration of playback via DirectDraw and is ideal for commercial games made with Flash.<br />
<a href="http://www.swfxxl.com" title="swfxxl" target="_blank">www.swfxxl.com</a></p>
<p><b>SWF DESKTOP</b><br />
A tool that will let you use SWF files as a desktop background.<br />
JPEGs? Tired. Flash MX? Wired!&#8221; That is the slogan for this product. Extremely easy to use as their screensaver tool ScreenTime but with just enough features. The program relies on Flash MX for any advanced functions, but just imagine the possibilities. Now you can make desktops that are even more pretty and useless than before, but you can also make smart desktops that keep you updated on news (did I hear someone say RSS-reader?) or include direct access to the company&#8217;s addressbook applications.<br />
<a href="http://www.screentime.com/" title="Screentime" target="_blank">http://www.screentime.com/</a></p>
<p><b>Swiff Player<br />
</b>Cool standalone player supporting hardware acceleration og both playback and some FX. FX include play as colored/black outlines, 3D Plane, 3D Slices (1 slice per layer in the SWF, fully rotatable!) and 3D Cube, plus &#8220;play as fast as possible&#8221;<a href="http://www.globfx.com/products/swfplayer/" target="_blank"><br />
GlobFX</a></p>
<p><b>Power Resolut</b><br />
Not a standalone player, but a hardware accelerated ActiveX plugin. Have your SWF files swap resolution of the users screen when they enter an enabled SWF file for increased playback speed. Based on the FullScreen player.</p>
<p><a href="http://www.globfx.com/products/swfplayer/" target="_blank"></a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/overflash.wordpress.com/4/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/overflash.wordpress.com/4/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/overflash.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/overflash.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/overflash.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/overflash.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/overflash.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/overflash.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/overflash.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/overflash.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/overflash.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/overflash.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/overflash.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/overflash.wordpress.com/4/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/overflash.wordpress.com/4/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/overflash.wordpress.com/4/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=overflash.wordpress.com&amp;blog=2601151&amp;post=4&amp;subd=overflash&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://overflash.wordpress.com/2008/01/24/swf-players/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5833fa31c9587997e99322f89f90de81?s=96&#38;d=identicon" medium="image">
			<media:title type="html">jpgdesign</media:title>
		</media:content>
	</item>
		<item>
		<title>Flash Performance Tips</title>
		<link>http://overflash.wordpress.com/2008/01/24/flash-performance-tips/</link>
		<comments>http://overflash.wordpress.com/2008/01/24/flash-performance-tips/#comments</comments>
		<pubDate>Thu, 24 Jan 2008 00:21:16 +0000</pubDate>
		<dc:creator>jpgdesign</dc:creator>
				<category><![CDATA[Flash Performance]]></category>
		<category><![CDATA[action]]></category>
		<category><![CDATA[actionscript 2]]></category>
		<category><![CDATA[ActionScript 3]]></category>
		<category><![CDATA[as2]]></category>
		<category><![CDATA[as3]]></category>
		<category><![CDATA[flash]]></category>
		<category><![CDATA[performance]]></category>
		<category><![CDATA[script]]></category>
		<category><![CDATA[tips]]></category>

		<guid isPermaLink="false">http://overflash.wordpress.com/2008/01/24/flash-performance-tips/</guid>
		<description><![CDATA[Optimizing Flash sites to run efficiently is 93% perspiration, 6% electricity, 4% evaporation, and 2% butterscotch ripple. Despite many coders’ frustrations over the performance of the player, there are ways you can make your site run more efficiently. Here’s a couple of tricks we use on a daily basis to keep things running smooth. Nothing [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=overflash.wordpress.com&amp;blog=2601151&amp;post=3&amp;subd=overflash&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>Optimizing Flash sites to run efficiently is 93% perspiration, 6%  electricity, 4% evaporation, and 2% butterscotch ripple. Despite many coders’  frustrations over the performance of the player, there are ways you can make  your site run more efficiently.</p>
<p>Here’s a couple of tricks we use on a daily basis to keep things running  smooth. Nothing fancy or complicated, but guaranteed success.</p>
<p><b><i>UPDATE:</i></b> Tera at <a href="http://www.trick7.com/blog" target="_blank">trick7</a> posted a Japanese  translation for our fine friends on the other side of the Pacific. We think  that’s pretty cool. Check it out <a href="http://www.trick7.com/blog/2006/12/14-090206.php" target="_blank">here</a>.</p>
<p><b>Masks Are Bad</b><br />
Well, they’re not all bad. Masks can be  exceptionally useful, as we all know by now. They’re the #1 performance killer  though. When you mask something, you force the player to decide what needs to be  rendered and hidden every single frame. So how do you get around without masks?  With a little bit of patience and tricky layering (such as making the background  the foreground with a giant hole cut out for where your viewable area is) you’ll  be able to get the same end result without the performance hit.</p>
<p><b>Alpha PNGs and Video</b><br />
Same deal as the masks. Sometimes  its unavoidable, but you’re still asking the player to figure out what to render  under the alpha. Sometimes we’ll make an alpha video at half size and then scale  it in Flash. You’d be surprised how good it still looks.</p>
<p>Oh, and with regards to alpha video: Try experimenting with PNG sequences in  place of the alpha video. Video usually performs a little better, but its always  worth looking into.</p>
<p><b>Frame Rate</b><br />
Despite all discussions otherwise, there is no  magic framerate. We use 25 or 30 because (as far as I know) we like it best. At  some point we tested and determined one was slightly better than the other, but  generally speaking this is not going to be the primary cause of a site running  slow. I wouldn’t generally advise going higher than 30 though, just because  you’re asking the player to render an awful lot awfully fast…</p>
<p><b>cacheAsBitmap and BitmapData</b><br />
Where possible use  cacheAsBitmap to rasterize vector items. You’ll force Flash to draw the symbol  one time and then never again. On the flip side, if you’re scaling or rotating a  symbol NEVER set it to cacheAsBitmap. Then you force Flash to render AND  recapture the raster every frame, making the process slower instead of  faster.</p>
<p>With sites like the <a href="http://www.sodarktheconofman.com/" target="_blank">Da Vinci Code</a> and <a href="http://www.nikeair.com/" target="_blank">Nike Air</a>, we would take a dynamic screenshot of the section,  draw it into an empty movieclip and then manipulate the screenshot to animate it  out. This is far, far faster than animating many elements out, or animating over  top of many elements. I highly recommend this practice.</p>
<p><b>_visible is better than _alpha</b><br />
_alpha = 0 and _visible =  false are totally different things. Alpha determines the opacity of a clip.  Visible determines whether or not the clip should actually be rendered by the  player. If you’re setting something all the way invisible, use the _visible  property.</p>
<p><b>onEnterFrame and setInterval</b><br />
When you’re through with  these processes, clear them from memory with onEnterFrame = null; and  clearInterval(myInterval); respectively. Leaving these around when you’re not  using them is like leaving the telephone off the hook when you’re done with a  call.</p>
<p><b>Pre-define your math</b><br />
Got a sine wave you’re about to  draw? Is it the same sine wave every time? Hard code the numbers into an array.  By doing the math for Flash, you’re saving some complex processes in advance. I  even experiment with using a Tween that I nextFrame() thru to get all of the  entries into an array before hand.</p>
<p><b>Silent Sound</b><br />
We use this as a last resort, but its a  trick worth mentioning. By putting a silent sound file on a seperate layer for  the duration of your timeline, setting it to stream and looping it, the player  will automatically drop frames to keep up with the sound.</p>
<p>Got any other good ones? I’m sure I didn’t list them all here</p>
<p>From: <a href="http://www.bigspaceship.com/" title="Bigspaceship" target="_blank">Bigspaceship </a></p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/overflash.wordpress.com/3/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/overflash.wordpress.com/3/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/overflash.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/overflash.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/overflash.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/overflash.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/overflash.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/overflash.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/overflash.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/overflash.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/overflash.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/overflash.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/overflash.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/overflash.wordpress.com/3/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/overflash.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/overflash.wordpress.com/3/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=overflash.wordpress.com&amp;blog=2601151&amp;post=3&amp;subd=overflash&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://overflash.wordpress.com/2008/01/24/flash-performance-tips/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5833fa31c9587997e99322f89f90de81?s=96&#38;d=identicon" medium="image">
			<media:title type="html">jpgdesign</media:title>
		</media:content>
	</item>
		<item>
		<title>About OverFlash</title>
		<link>http://overflash.wordpress.com/2008/01/23/hello-world/</link>
		<comments>http://overflash.wordpress.com/2008/01/23/hello-world/#comments</comments>
		<pubDate>Wed, 23 Jan 2008 23:56:18 +0000</pubDate>
		<dc:creator>jpgdesign</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false"></guid>
		<description><![CDATA[As a programmer/designer myself I started to get in trouble when ActionScript 3 (AS3), came and took the place of ActionScript 2 (AS2). I hardly found enough resources for my needs and the only thing that helped, was my background, because if you look at Java Programming you might get surprised with so many similarities. [...]<img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=overflash.wordpress.com&amp;blog=2601151&amp;post=1&amp;subd=overflash&amp;ref=&amp;feed=1" width="1" height="1" />]]></description>
			<content:encoded><![CDATA[<p>As a programmer/designer myself I started to get in trouble when ActionScript 3 (AS3), came and took the place of ActionScript 2 (AS2).<span>  </span>I hardly found enough resources for my needs and the only thing that helped, was my background, because if you look at Java Programming you might get surprised with so many similarities. But finally, Flash is on the right track and is starting to assume itself as a real programming language. <span> </span>This blog is a list of researches I did to solve most of my AS3 nightmares and I do hope you´ll find it useful and not spend so many time searching. My name is too hard to spell, because I´m Portuguese so the best name you can call me is “overflash”.</p>
<br /><img alt="" border="0" src="http://feeds.wordpress.com/1.0/categories/overflash.wordpress.com/1/" /> <img alt="" border="0" src="http://feeds.wordpress.com/1.0/tags/overflash.wordpress.com/1/" /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/overflash.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/overflash.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godelicious/overflash.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/delicious/overflash.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gofacebook/overflash.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/facebook/overflash.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gotwitter/overflash.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/twitter/overflash.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gostumble/overflash.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/stumble/overflash.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/godigg/overflash.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/digg/overflash.wordpress.com/1/" /></a> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/goreddit/overflash.wordpress.com/1/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/reddit/overflash.wordpress.com/1/" /></a> <img alt="" border="0" src="http://stats.wordpress.com/b.gif?host=overflash.wordpress.com&amp;blog=2601151&amp;post=1&amp;subd=overflash&amp;ref=&amp;feed=1" width="1" height="1" />]]></content:encoded>
			<wfw:commentRss>http://overflash.wordpress.com/2008/01/23/hello-world/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
	
		<media:content url="http://1.gravatar.com/avatar/5833fa31c9587997e99322f89f90de81?s=96&#38;d=identicon" medium="image">
			<media:title type="html">jpgdesign</media:title>
		</media:content>
	</item>
	</channel>
</rss>
