<?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/"
	>
<channel>
	<title>Flex notes &#187; Actionscript</title>
	<atom:link href="http://www.robertbak.com/wordpress/category/actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.robertbak.com/wordpress</link>
	<description>My notes on Flex related topics</description>
	<lastBuildDate>Thu, 10 Nov 2011 10:49:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Generating PDF-s on the client side (including non-latin chars)</title>
		<link>http://www.robertbak.com/wordpress/2011/11/generating-pdf-s-on-the-client-side-including-non-latin-chars/</link>
		<comments>http://www.robertbak.com/wordpress/2011/11/generating-pdf-s-on-the-client-side-including-non-latin-chars/#comments</comments>
		<pubDate>Thu, 10 Nov 2011 09:59:54 +0000</pubDate>
		<dc:creator>Robert Bak</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[Flex 4]]></category>
		<category><![CDATA[pdf]]></category>
		<category><![CDATA[polskie znaki]]></category>
		<category><![CDATA[purepdf]]></category>
		<guid isPermaLink="false">http://www.robertbak.com/wordpress/?p=435</guid>
		<description><![CDATA[This seems to come up every once in a while, especially outside US and UK. People start looking at the PDF generating libraries, and find that when they use a non-latin character it stops to work. It needs some special care but it is really easy to get it working, I&#8217;m going to show how [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>This seems to come up every once in a while, especially outside US and UK. People start looking at the PDF generating libraries, and find that when they use a non-latin character it stops to work. It needs some special care but it is really easy to get it working, I&#8217;m going to show how to do it, step by step, when using purepdf (a port of iText</p>
<h3>Getting ready</h3>
<p>You can find purepdf on <a href="http://code.google.com/p/purepdf/downloads/list">google code</a>. Once you download the zip you&#8217;ll find <em>purePDF.swc</em> and <em>purePDFont.swc</em>, for this example you&#8217;ll only need the first one so put it in your projects lib folder and you&#8217;re ready.<br />
The next thing you need is a font to use, I&#8217;ll be using <em>Ubuntu-L.ttf</em> which can be downloaded from <a href="http://font.ubuntu.com/">font.ubuntu.com</a>, just drop it in the project. You can use any font, as long as it supports the charset you&#8217;ll be using. At this point my project looks something like this:<br />
<a href="http://www.robertbak.com/wordpress/wp-content/uploads/2011/11/testingPDF1.png"><img src="http://www.robertbak.com/wordpress/wp-content/uploads/2011/11/testingPDF1.png" alt="" title="testingPDF1" width="233" height="206" class="aligncenter size-full wp-image-437" /></a><br />
<span id="more-435"></span></p>
<h3>Flex stuff</h3>
<p>To make the example work, we need some basic UI which, in the example, begins and ends with:</p>
<div class="codecolorer-container actionscript geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&lt;</span>s:TextInput id=<span style="color: #ff0000;">&quot;TI&quot;</span> <span style="color: #0066CC;">text</span>=<span style="color: #ff0000;">&quot;ążółć - works!&quot;</span> <span style="color: #66cc66;">/&gt;</span><br />
<span style="color: #66cc66;">&lt;</span>s:<span style="color: #0066CC;">Button</span> click=<span style="color: #ff0000;">&quot;createPDF()&quot;</span> label=<span style="color: #ff0000;">&quot;Generate PDF&quot;</span> y=<span style="color: #ff0000;">&quot;22&quot;</span> <span style="color: #66cc66;">/&gt;</span></div></div>
<p>As you can see, the text has some polish characters (as Flex text is UTF encoded there&#8217;s no problem with putting what you need there). The other thing worth noting is that we&#8217;re going to be creating the PDF after a click, that&#8217;s important as we want to save the pdf to the user system and that&#8217;s only possible when the <em>save</em> function is being called after an user interaction.</p>
<p>What&#8217;s left to do now is embedding the font:</p>
<div class="codecolorer-container actionscript geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#91;</span>Embed<span style="color: #66cc66;">&#40;</span>source=<span style="color: #ff0000;">&quot;Ubuntu-L.ttf&quot;</span>, mimeType=<span style="color: #ff0000;">&quot;application/octet-stream&quot;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#93;</span><br />
<span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> embeddedFont: <span style="color: #000000; font-weight: bold;">Class</span>;</div></div>
<p>The mineType is required to stop Flex from handling the fonts as a font  <a class="simple-footnote" title="Without it you&#8217;ll get a message like &#8220;font transcoding requires you to specify &#8216;fontName&#8217; and one of &#8216;source&#8217;, &#8216;systemFont&#8217;, or &#8216;sourceList&#8217;&#8221;" id="return-note-435-1" href="#note-435-1"><sup>1</sup></a>. That&#8217;s all the setup we need no we can get to the purepdf code.</p>
<h3>Purepdf code</h3>
<p>The code that&#8217;s needed to start using the font and generatign the pdf takes around 10 lines, I&#8217;ll paste it first and than add comments.</p>
<div class="codecolorer-container actionscript geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><table cellspacing="0" cellpadding="0"><tbody><tr><td style="padding:5px;text-align:center;color:#888888;background-color:#EEEEEE;border-right: 1px solid #9F9F9F;font: normal 12px/1.4em Monaco, Lucida Console, monospace;"><div>1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br />10<br />11<br />12<br />13<br />14<br />15<br />16<br />17<br />18<br />19<br />20<br />21<br />22<br />23<br /></div></td><td><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">protected <span style="color: #000000; font-weight: bold;">function</span> createPDF<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// Prepare the font </span><br />
&nbsp; &nbsp;FontsResourceFactory.<span style="color: #006600;">getInstance</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #006600;">registerFont</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;newFont.otf&quot;</span>, <span style="color: #000000; font-weight: bold;">new</span> embeddedFont<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">var</span> bf: BaseFont = BaseFont.<span style="color: #006600;">createFont</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;newFont.otf&quot;</span>, BaseFont.<span style="color: #006600;">IDENTITY_H</span>, BaseFont.<span style="color: #006600;">EMBEDDED</span> <span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// parameters are:</span><br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// family, font-size, style, color, BaseFont</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">var</span> <span style="color: #0066CC;">font</span>: org.<span style="color: #006600;">purepdf</span>.<span style="color: #0066CC;">Font</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp;= <span style="color: #000000; font-weight: bold;">new</span> org.<span style="color: #006600;">purepdf</span>.<span style="color: #0066CC;">Font</span><span style="color: #66cc66;">&#40;</span> org.<span style="color: #006600;">purepdf</span>.<span style="color: #0066CC;">Font</span>.<span style="color: #0066CC;">UNDEFINED</span>, <span style="color: #cc66cc;">24</span>, -<span style="color: #cc66cc;">1</span>, RGBColor.<span style="color: #006600;">BLACK</span>, bf <span style="color: #66cc66;">&#41;</span>;<br />
<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">var</span> buffer:ByteArray = <span style="color: #000000; font-weight: bold;">new</span> ByteArray<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">var</span> writer:PdfWriter = PdfWriter.<span style="color: #006600;">create</span><span style="color: #66cc66;">&#40;</span> buffer, PageSize.<span style="color: #006600;">A4</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">var</span> doc:PdfDocument = writer.<span style="color: #006600;">pdfDocument</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp;doc.<span style="color: #006600;">open</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// When creating a paragraph &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// add the font parameter</span><br />
&nbsp; &nbsp;doc.<span style="color: #0066CC;">add</span><span style="color: #66cc66;">&#40;</span> <span style="color: #000000; font-weight: bold;">new</span> Paragraph<span style="color: #66cc66;">&#40;</span>TI.<span style="color: #0066CC;">text</span>,<span style="color: #0066CC;">font</span><span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;doc.<span style="color: #0066CC;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// save the file</span><br />
&nbsp; &nbsp;<span style="color: #000000; font-weight: bold;">var</span> f: FileReference = <span style="color: #000000; font-weight: bold;">new</span> FileReference<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;f.<span style="color: #006600;">save</span><span style="color: #66cc66;">&#40;</span> buffer, <span style="color: #ff0000;">&quot;test.pdf&quot;</span> <span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></td></tr></tbody></table></div>
<p>In lines <em>4-9</em> we&#8217;re using the embedded font file to create an instance of an <em>org.purepdf.Font</em>, that&#8217;s all you need to do to start using the font. The rest of the code is standard purepdf code.</p>
<p>11 &#8211; creates a buffer to hold the pdf file</p>
<p>12 &#8211; creates a <a href="http://www.sephiroth.it/purepdf/asdoc-output/org/purepdf/pdf/PdfWriter.html#create()">PDFWriter</a> linked with that buffer and sets the size of the pdf page</p>
<p>13 &#8211; gets the <a href="http://www.sephiroth.it/purepdf/asdoc-output/org/purepdf/pdf/PdfDocument.html">PDFDocument</a> from the writer</p>
<p>14 &#8211; opens the document</p>
<p><strong>15</strong> &#8211; adds a <a href="http://www.sephiroth.it/purepdf/asdoc-output/org/purepdf/elements/Paragraph.html">paragraph</a> to the open document. Here&#8217;s the important change if you already have some code written, you need to set the second parameter which is the font you&#8217;re wanting to use. If it supports the chars you&#8217;re using there will be no problem.</p>
<p>16 &#8211; closes the document</p>
<p>and 20-21 save the document to the user filesystem.</p>
<p>That&#8217;s it, the PDF you get in result looks something like this:<br />
<a href="http://www.robertbak.com/wordpress/wp-content/uploads/2011/11/testingPDF2.png"><img src="http://www.robertbak.com/wordpress/wp-content/uploads/2011/11/testingPDF2-300x59.png" alt="" title="testingPDF2" width="300" height="59" class="aligncenter size-medium wp-image-450" /></a></p>
<p>Hopefully this get&#8217;s people started. You can find many more examples and tutorials on the <a href="http://code.google.com/p/purepdf/w/list">google code</a> page for the project.</p>
<p><a href="http://dl.dropbox.com/u/207892/Flex/TestingPurePDF.fxp">Download source code.</a></p>
<div class="shr-publisher-435"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic --><div class="simple-footnotes"><p class="notes">Notes:</p><ol><li id="note-435-1">Without it you&#8217;ll get a message like &#8220;font transcoding requires you to specify &#8216;fontName&#8217; and one of &#8216;source&#8217;, &#8216;systemFont&#8217;, or &#8216;sourceList&#8217;&#8221; <a href="#return-note-435-1">&#8617;</a></li></ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.robertbak.com/wordpress/2011/11/generating-pdf-s-on-the-client-side-including-non-latin-chars/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Basic socket server in AIR</title>
		<link>http://www.robertbak.com/wordpress/2011/10/example-basic-socket-server-in-air/</link>
		<comments>http://www.robertbak.com/wordpress/2011/10/example-basic-socket-server-in-air/#comments</comments>
		<pubDate>Tue, 25 Oct 2011 11:04:56 +0000</pubDate>
		<dc:creator>Robert Bak</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Flex]]></category>
		<guid isPermaLink="false">http://www.robertbak.com/wordpress/?p=316</guid>
		<description><![CDATA[The socket api is relatively simple, but I couldn&#8217;t find a simple step by step tutorial on getting sockets working. The basic idea was to create an app that can send text to connected clients (which I&#8217;ll be using to send code while teaching a Flex class), and getting that basic functionality working is amazingly [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>The socket api is relatively simple, but I couldn&#8217;t find a simple step by step tutorial on getting sockets working. The basic idea was to create an app that can send text to connected clients (which I&#8217;ll be using to send code while teaching a Flex class), and getting that basic functionality working is amazingly simple with AIR 2+. </p>
<h3>The config</h3>
<p>To make life easier, first I&#8217;ve created a class that will hold the configuration for the socket, it&#8217;s nothing interesting, but as it will be used later, I&#8217;ll put it at the top. There are a few things to keep in mind while choosing the port &#8211; it needs to be smaller than 65536 and should be bigger than 1024 (see <a href="http://help.adobe.com/en_US/air/reference/html/flash/net/ServerSocket.html#bind()">docs</a>) and ports can&#8217;t be shared, so you need to make sure that the port you&#8217;re choosing isn&#8217;t already used <a class="simple-footnote" title="Remember to close the socket when closing the app, otherwise you might run into trouble when launching it again" id="return-note-316-1" href="#note-316-1"><sup>1</sup></a>.</p>
<div class="codecolorer-container actionscript geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SocketConfig<br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0066CC;">static</span> <span style="color: #0066CC;">public</span> const port:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">9231</span>;<br />
&nbsp; &nbsp;<span style="color: #0066CC;">static</span> <span style="color: #0066CC;">public</span> const address:<span style="color: #0066CC;">String</span> = <span style="color: #ff0000;">&quot;127.0.0.1&quot;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></div>
<h3>The server &#8211; setup</h3>
<p>Creating the server is really easy. We just need to set up an instance of the <a href="http://help.adobe.com/en_US/air/reference/html/flash/net/ServerSocket.html">ServerSocket</a> class, and start listening and handling incoming connections.</p>
<div class="codecolorer-container actionscript geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// create a server socket</span><br />
protected <span style="color: #000000; font-weight: bold;">var</span> socket:ServerSocket = <span style="color: #000000; font-weight: bold;">new</span> ServerSocket<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
protected <span style="color: #000000; font-weight: bold;">function</span> createServer<span style="color: #66cc66;">&#40;</span>event:FlexEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// bind the socket to a given address and port</span><br />
&nbsp; &nbsp;socket.<span style="color: #006600;">bind</span><span style="color: #66cc66;">&#40;</span>SocketConfig.<span style="color: #006600;">port</span>,SocketConfig.<span style="color: #006600;">address</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// the CONNECT event is dispatched after a client connects</span><br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// to the socket, make sure we handle it</span><br />
&nbsp; &nbsp;socket.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>ServerSocketConnectEvent.<span style="color: #0066CC;">CONNECT</span>,<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;clientConnectedHandler<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// start listening for connections</span><br />
&nbsp; &nbsp;socket.<span style="color: #006600;">listen</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p><span id="more-316"></span><br />
As it&#8217;s supposed to push data to multiple clients, here&#8217;s the code to keep track of the connections,</p>
<div class="codecolorer-container actionscript geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #66cc66;">&#91;</span>Bindable<span style="color: #66cc66;">&#93;</span> <span style="color: #808080; font-style: italic;">// for keeping track of the clients</span><br />
protected <span style="color: #000000; font-weight: bold;">var</span> clientSockets:ArrayCollection = <span style="color: #000000; font-weight: bold;">new</span> ArrayCollection<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
<span style="color: #808080; font-style: italic;">// adds the client to the list and adds the disconnect handler&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </span><br />
<span style="color: #000000; font-weight: bold;">function</span> clientConnectedHandler<span style="color: #66cc66;">&#40;</span>event:ServerSocketConnectEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;clientSockets.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span>event.<span style="color: #006600;">socket</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;event.<span style="color: #006600;">socket</span>.<span style="color: #006600;">addEventListener</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span>Event.<span style="color: #0066CC;">CLOSE</span>,clientDisconnectedHandler<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
<span style="color: #000000; font-weight: bold;">function</span> clientDisconnectedHandler<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;clientSockets.<span style="color: #006600;">removeItemAt</span><span style="color: #66cc66;">&#40;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;clientSockets.<span style="color: #006600;">getItemIndex</span><span style="color: #66cc66;">&#40;</span>event.<span style="color: #0066CC;">target</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;event.<span style="color: #0066CC;">target</span>.<span style="color: #006600;">removeEventListener</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #66cc66;">&#40;</span>Event.<span style="color: #0066CC;">CLOSE</span>,clientDisconnectedHandler<span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></div>
<h3>The client &#8211; setup</h3>
<p>Setting up a client is as easy, if not easier. You just need to create a <a href="http://help.adobe.com/en_US/air/reference/html/flash/net/Socket.html">Socket</a>, add event listeners to link some logic to it and connect to the server (using it&#8217;s address and port)</p>
<div class="codecolorer-container actionscript geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// create our client socket</span><br />
protected <span style="color: #000000; font-weight: bold;">var</span> socket:Socket = <span style="color: #000000; font-weight: bold;">new</span> Socket<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<br />
protected <span style="color: #000000; font-weight: bold;">function</span> createSocket<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// get notified when the socket connects</span><br />
&nbsp; &nbsp;socket.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>Event.<span style="color: #0066CC;">CONNECT</span>,socketConnected<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// get notified when there's data</span><br />
&nbsp; &nbsp;socket.<span style="color: #006600;">addEventListener</span><span style="color: #66cc66;">&#40;</span>ProgressEvent.<span style="color: #006600;">SOCKET_DATA</span>,socketData<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// connect</span><br />
&nbsp; &nbsp;socket.<span style="color: #0066CC;">connect</span><span style="color: #66cc66;">&#40;</span>SocketConfig.<span style="color: #006600;">address</span>,SocketConfig.<span style="color: #006600;">port</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>We can create functions with trace statements to check if the connection works and, as the only thing this server will be sending will be strings, output the data is being sent.</p>
<div class="codecolorer-container actionscript geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">protected <span style="color: #000000; font-weight: bold;">function</span> socketConnected<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;client - socket connected&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
protected <span style="color: #000000; font-weight: bold;">function</span> socketData<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">e</span>:ProgressEvent<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;client - socket data&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #808080; font-style: italic;">// read the string from the socket</span><br />
&nbsp; &nbsp;<span style="color: #0066CC;">trace</span><span style="color: #66cc66;">&#40;</span>socket.<span style="color: #006600;">readUTF</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></div>
<h3>The server &#8211; finishing</h3>
<p>The last thing we need to do is send the data to sockets, we&#8217;re going to use the <em><a href="http://help.adobe.com/en_US/air/reference/html/flash/net/Socket.html#writeUTF()">writeUTF</a></em> <a class="simple-footnote" title="writeUTF has a limit on string length &#8211; it needs to be shorter than 65536" id="return-note-316-2" href="#note-316-2"><sup>2</sup></a> of the socket. Notice that this function is called on each of the connected sockets (stored in the <em>clientSockets</em> collection) and not on our <em>ServerSocket</em> instance. It&#8217;s also important to flush the data to the socket once finished to make sure the data is sent.</p>
<div class="codecolorer-container actionscript geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">protected <span style="color: #000000; font-weight: bold;">function</span> writeDataToSockets<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp;<span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> sock:Socket <span style="color: #b1b100;">in</span> clientSockets<span style="color: #66cc66;">&#41;</span><span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; sock.<span style="color: #006600;">writeUTF</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;test&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; sock.<span style="color: #0066CC;">flush</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp;<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>That&#8217;s it, now we have a fully working server which can push data to clients over TCP. </p>
<p><a href="http://www.robertbak.com/wordpress/wp-content/uploads/2011/10/AIRSocket.png"><img src="http://www.robertbak.com/wordpress/wp-content/uploads/2011/10/AIRSocket.png" alt="" title="AIRSocket screenshot" width="468" height="121" class="aligncenter size-full wp-image-375" /></a></p>
<p>Please remember that this is a very basic example with no code for error handling, etc. Just enough code to get it working. The very last thing to do is make sure we release the port once the server application closes.</p>
<div class="codecolorer-container actionscript geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">protected <span style="color: #000000; font-weight: bold;">function</span> applicationCloseHandler<span style="color: #66cc66;">&#40;</span>event:Event<span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; socket.<span style="color: #0066CC;">close</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>The source code has some additional code for the UI.<br />
<a href="http://robertbak.com/flexsamples/AIRSocket.fxp">Download source code.</a></p>
<div class="shr-publisher-316"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic --><div class="simple-footnotes"><p class="notes">Notes:</p><ol><li id="note-316-1">Remember to close the socket when closing the app, otherwise you might run into trouble when launching it again <a href="#return-note-316-1">&#8617;</a></li><li id="note-316-2">writeUTF has a limit on string length &#8211; it needs to be shorter than 65536 <a href="#return-note-316-2">&#8617;</a></li></ol></div>]]></content:encoded>
			<wfw:commentRss>http://www.robertbak.com/wordpress/2011/10/example-basic-socket-server-in-air/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Things you might not know about calling actionscript functions</title>
		<link>http://www.robertbak.com/wordpress/2010/02/things-you-might-not-know-about-calling-actionscript-functions/</link>
		<comments>http://www.robertbak.com/wordpress/2010/02/things-you-might-not-know-about-calling-actionscript-functions/#comments</comments>
		<pubDate>Fri, 19 Feb 2010 22:20:21 +0000</pubDate>
		<dc:creator>Robert Bak</dc:creator>
				<category><![CDATA[Actionscript]]></category>
		<category><![CDATA[Function]]></category>
		<guid isPermaLink="false">http://www.robertbak.com/wordpress/?p=38</guid>
		<description><![CDATA[OK, my guess would be that everybody used a function at one point or another. But there are some things which are less than obvious about an actionscript function. Let&#8217;s take a quick look. Calling functions Let&#8217;s create a simple function which will accept any number of parameters. function sum&#40;...arg&#41;:int &#123; &#160; &#160; &#160; var [...]]]></description>
			<content:encoded><![CDATA[<!-- Start Shareaholic LikeButtonSetTop Automatic --><!-- End Shareaholic LikeButtonSetTop Automatic --><p>OK, my guess would be that everybody used a function at one point or another. But there are some things which are less than obvious about an actionscript function. Let&#8217;s take a quick look.</p>
<h3>Calling functions</h3>
<p>Let&#8217;s create a simple function which will accept any number of parameters.</p>
<div class="codecolorer-container actionscript geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> sum<span style="color: #66cc66;">&#40;</span>...<span style="color: #006600;">arg</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">int</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">var</span> val:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">0</span>; <br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> i:<span style="color: #0066CC;">int</span> <span style="color: #b1b100;">in</span> arg<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val+=i;<br />
&nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">return</span> val;<br />
<span style="color: #66cc66;">&#125;</span><br />
<span style="color: #808080; font-style: italic;">// The usual way to call it</span><br />
sum<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span>;</div></div>
<p>But that&#8217;s something everybody knew how to do. The interesting thing is that in Actionscript 3.0 there is a class called <em>Function</em> which also has it&#8217;s own methods defined, and all the functions are instances of that class. Most of the methods are inherited from <em>Object</em> but there are two defined for it, which are used to achieve a similar thing &#8211; calling the function, this are <strong>apply()</strong> and <strong>call()</strong>. The first parameter of both functions is almost always ignored [1]. </p>
<p>The definition of call &#8211; <code class="codecolorer actionscript default"><span class="actionscript"><span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span>thisArg:<span style="color: #66cc66;">*</span>, ...<span style="color: #006600;">args</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #66cc66;">*</span></span></code> shows that it also takes any number of parameters, than it uses them as arguments, so, as the first parameter is ignored &#8211; <code class="codecolorer actionscript default"><span class="actionscript">sum.<span style="color: #0066CC;">call</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span>,<span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span></span></code> is the same as <code class="codecolorer actionscript default"><span class="actionscript">sum<span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#41;</span></span></code>.<br />
The problem with calling functions like this is that you actually need to know how many parameters you&#8217;re going to use. </p>
<p>Luckily <strong>apply()</strong> has an array as a second parameter. Which means you can call a function, and pass any number of parameters as an Array. Quick sample of achieving the exact same functionality as before using <em>apply</em>.</p>
<div class="codecolorer-container actionscript geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #808080; font-style: italic;">// this array can be dynamically generated</span><br />
<span style="color: #000000; font-weight: bold;">var</span> params:<span style="color: #0066CC;">Array</span> = <span style="color: #66cc66;">&#91;</span><span style="color: #cc66cc;">0</span>,<span style="color: #cc66cc;">1</span>,<span style="color: #cc66cc;">2</span><span style="color: #66cc66;">&#93;</span>;<br />
sum.<span style="color: #0066CC;">apply</span><span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">null</span>,params<span style="color: #66cc66;">&#41;</span>;</div></div>
<p>Which is much more flexible.</p>
<h3>arguments variable</h3>
<p>One more thing which is worth knowing is that inside functions you can access the <code class="codecolorer text default"><span class="text">arguments</span></code> variable. It&#8217;s holds all the variables passed to the function, with the limitation that it doesn&#8217;t work when using the <code class="codecolorer actionscript default"><span class="actionscript">...<span style="color: #006600;">arg</span></span></code> notation in the function definition. So this will not work:</p>
<pre>function sum(...arg):int
{
      trace (arguments);
      // throws an error
      [...]
}</pre>
<p>But it will work with functions without the <code class="codecolorer actionscript default"><span class="actionscript">...<span style="color: #006600;">arg</span></span></code> in the definition, for example this prints out both strings:</p>
<div class="codecolorer-container actionscript geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">function</span> printStrings<span style="color: #66cc66;">&#40;</span>s:<span style="color: #0066CC;">String</span>,s2:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">arguments</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>Apart from that this variable also holds the reference to the currently executing function in the <code class="codecolorer actionscript default"><span class="actionscript"><span style="color: #0066CC;">arguments</span>.<span style="color: #0066CC;">callee</span></span></code> property. Things like this come in handy sometimes.</p>
<h3>Sample</h3>
<p>Using all the tools described (<em>callee, arguments, apply</em>) we can build a quick sample, a class that records the calls to it&#8217;s functions in an easy way, and is able to replay them when needed.</p>
<p><em>CallRecorder.as</em></p>
<div class="codecolorer-container actionscript geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">package<br />
<span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #0066CC;">import</span> mx.<span style="color: #006600;">collections</span>.<span style="color: #006600;">ArrayCollection</span>;<br />
&nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">class</span> CallRecorder<br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Just a sample function</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> printJohn<span style="color: #66cc66;">&#40;</span>s:<span style="color: #0066CC;">String</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rememberTheCall<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">arguments</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'John says '</span> + s<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Just a another sample function</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// notice that it has a different set of arguments</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> printBob<span style="color: #66cc66;">&#40;</span>s:<span style="color: #0066CC;">String</span>,times:<span style="color: #0066CC;">int</span> = <span style="color: #cc66cc;">1</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rememberTheCall<span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">arguments</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">while</span> <span style="color: #66cc66;">&#40;</span>times-- <span style="color: #66cc66;">&gt;</span> <span style="color: #cc66cc;">0</span><span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'Bob says '</span> + s<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/** Replays all the calls in memory **/</span> <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">public</span> <span style="color: #000000; font-weight: bold;">function</span> replay<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; replayIsOn = <span style="color: #000000; font-weight: bold;">true</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;--- Starting the replay ---&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// Notice we actually don't need to worry </span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">// about the arguments &nbsp;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">for</span> <span style="color: #b1b100;">each</span> <span style="color: #66cc66;">&#40;</span><span style="color: #000000; font-weight: bold;">var</span> arg:<span style="color: #0066CC;">Object</span> <span style="color: #b1b100;">in</span> memory<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#40;</span>arg.<span style="color: #0066CC;">callee</span><span style="color: #66cc66;">&#41;</span>.<span style="color: #0066CC;">apply</span><span style="color: #66cc66;">&#40;</span><span style="color: #0066CC;">this</span>,arg<span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; replayIsOn = <span style="color: #000000; font-weight: bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">trace</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;--- Stoping the replay ---&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/** True if currently &nbsp;replaying **/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> replayIsOn:<span style="color: #0066CC;">Boolean</span> = <span style="color: #000000; font-weight: bold;">false</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/** Used for storing the history of function calls **/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">var</span> memory:ArrayCollection = <span style="color: #000000; font-weight: bold;">new</span> ArrayCollection<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #808080; font-style: italic;">/** Stores a call in memory, if not called while replay **/</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #0066CC;">private</span> <span style="color: #000000; font-weight: bold;">function</span> rememberTheCall<span style="color: #66cc66;">&#40;</span>passedArguments:<span style="color: #0066CC;">Object</span><span style="color: #66cc66;">&#41;</span>:<span style="color: #0066CC;">void</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #b1b100;">if</span> <span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">!</span>replayIsOn<span style="color: #66cc66;">&#41;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memory.<span style="color: #006600;">addItem</span><span style="color: #66cc66;">&#40;</span>passedArguments<span style="color: #66cc66;">&#41;</span>;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #66cc66;">&#125;</span><br />
<span style="color: #66cc66;">&#125;</span></div></div>
<p>Sample execution:</p>
<div class="codecolorer-container actionscript geshi" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="actionscript codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">var</span> recorder:CallRecorder = <span style="color: #000000; font-weight: bold;">new</span> CallRecorder<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
recorder.<span style="color: #006600;">printBob</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;I do&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
recorder.<span style="color: #006600;">printJohn</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;I do too&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
recorder.<span style="color: #006600;">printBob</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;:)&quot;</span>,<span style="color: #cc66cc;">3</span><span style="color: #66cc66;">&#41;</span>;<br />
recorder.<span style="color: #006600;">printJohn</span><span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">&quot;:)&quot;</span><span style="color: #66cc66;">&#41;</span>;<br />
recorder.<span style="color: #006600;">replay</span><span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>;<br />
<span style="color: #808080; font-style: italic;">// results in</span><br />
<span style="color: #808080; font-style: italic;">// Bob says I do</span><br />
<span style="color: #808080; font-style: italic;">// John says I do too</span><br />
<span style="color: #808080; font-style: italic;">// Bob says :)</span><br />
<span style="color: #808080; font-style: italic;">// Bob says :)</span><br />
<span style="color: #808080; font-style: italic;">// Bob says :)</span><br />
<span style="color: #808080; font-style: italic;">// John says :)</span><br />
<span style="color: #808080; font-style: italic;">// --- Starting the replay ---</span><br />
<span style="color: #808080; font-style: italic;">// Bob says I do</span><br />
<span style="color: #808080; font-style: italic;">// John says I do too</span><br />
<span style="color: #808080; font-style: italic;">// Bob says :)</span><br />
<span style="color: #808080; font-style: italic;">// Bob says :)</span><br />
<span style="color: #808080; font-style: italic;">// Bob says :)</span><br />
<span style="color: #808080; font-style: italic;">// John says :)</span><br />
<span style="color: #808080; font-style: italic;">// --- Stoping the replay ---</span></div></div>
<p>Not a great piece of software but hopefully interesting.<br />
&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;<br />
[1] The only case it&#8217;s not ignored which I could find is when using anonymous functions.<br />
<a href="http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/Function.html">LiveDocs on Function class</a><br />
<a href="http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/arguments.html">LiveDocs on arguments class</a></p>
<div class="shr-publisher-38"></div><!-- Start Shareaholic LikeButtonSetBottom Automatic --><!-- End Shareaholic LikeButtonSetBottom Automatic -->]]></content:encoded>
			<wfw:commentRss>http://www.robertbak.com/wordpress/2010/02/things-you-might-not-know-about-calling-actionscript-functions/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

