﻿<?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>Armando Leopoldo Keller &#187; conexao</title>
	<atom:link href="http://armandokeller.com/blog/tag/conexao-as-30/feed/" rel="self" type="application/rss+xml" />
	<link>http://armandokeller.com/blog</link>
	<description>Actionscript, AIR, Python, Engenharia Elétrica....</description>
	<lastBuildDate>Tue, 24 Jan 2012 19:26:51 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Conexão AS3 com python via socket (simplificado)</title>
		<link>http://armandokeller.com/blog/2009/01/conexo-as3-com-python-via-socket-simplificado/</link>
		<comments>http://armandokeller.com/blog/2009/01/conexo-as3-com-python-via-socket-simplificado/#comments</comments>
		<pubDate>Sat, 17 Jan 2009 17:30:41 +0000</pubDate>
		<dc:creator>Armando Leopoldo Keller</dc:creator>
				<category><![CDATA[AIR]]></category>
		<category><![CDATA[AS 3.0]]></category>
		<category><![CDATA[conexao]]></category>
		<category><![CDATA[Flash geral]]></category>
		<category><![CDATA[Flex]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[Socket]]></category>
		<category><![CDATA[Actionscript 3]]></category>

		<guid isPermaLink="false">http://alkeller.wordpress.com/2009/01/17/conexo-as3-com-python-via-socket-simplificado/</guid>
		<description><![CDATA[Algumas vezes me perguntei como fazer algumas coisas mais complexas em AIR de uma maneira rapida e segura, como por exemplo pegar informaçoes do usuario, conectar com um banco de dados (sem ser o nativo do AIR) como oracle,mssql… executar linha de comando, e outras coisas que só seriam possiveis utilizando alguma outra linguagem mais poderosa. Levando em conta que isto realmente era necessario para aumentar o “poder” do AIR e que o AMF fazia algo parecido, só que para a internet, resolvi criar uma classe em AS e uma função em Python que facilita a comunicação entre os dois (deixe o servidor python rodando de fundo e chame as funçoes dele :D )]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button socialize-in-button-vertical"><script type="text/javascript">
			<!-- 
				tweetmeme_url = "http://armandokeller.com/blog/2009/01/conexo-as3-com-python-via-socket-simplificado/";
				tweetmeme_source = "tweetmeme";
				tweetmeme_style = "";
				
			//-->
			</script>
                        <script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script></div><div class="socialize-in-button socialize-in-button-vertical"><script>
			<!-- 
			var fbShare = {
				url: "http://armandokeller.com/blog/2009/01/conexo-as3-com-python-via-socket-simplificado/",
				size: "large",
				google_analytics: "true"
			}
			//-->
			</script>
                        <script src="http://widgets.fbshare.me/files/fbshare.js"></script></div><div class="socialize-in-button socialize-in-button-vertical"><script type="text/javascript"><!-- 
		(function() {
		var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0];
		s.type = 'text/javascript';
		s.async = true;
		s.src = 'http://widgets.digg.com/buttons.js';
		s1.parentNode.insertBefore(s, s1);
		})();
		//-->
		</script><a class="DiggThisButton " href="http://digg.com/submit?url=http%3A%2F%2Farmandokeller.com%2Fblog%2F2009%2F01%2Fconexo-as3-com-python-via-socket-simplificado%2F"></a></div><div class="socialize-in-button socialize-in-button-vertical"><div class="delicious-button"><div class="del-top"><span id="41">0</span>saves</div><div class="del-bot"><a href="http://delicious.com/save" onclick="window.open('http://delicious.com/save?v=5&noui&jump=close&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title), 'delicious','toolbar=no,width=550,height=550'); return false;">Save</a></div></div>
		<script>
			<!-- 
			function displayURL(data) { var urlinfo = data[0]; if (!urlinfo.total_posts) return;document.getElementById('41').innerHTML = urlinfo.total_posts;}
			//-->
		</script>
		<script src = "http://badges.del.icio.us/feeds/json/url/data?url=http://armandokeller.com/blog/2009/01/conexo-as3-com-python-via-socket-simplificado/&amp;callback=displayURL"></script></div></div><p>Algumas vezes me perguntei como fazer algumas coisas mais complexas em AIR de uma maneira rapida e segura, como por exemplo pegar informaçoes do usuario, conectar com um banco de dados (sem ser o nativo do AIR) como oracle,mssql… executar linha de comando, e outras coisas que só seriam possiveis utilizando alguma outra linguagem mais poderosa. Levando em conta que isto realmente era necessario para aumentar o “poder” do AIR e que o AMF fazia algo parecido, só que para a internet, resolvi criar uma classe em AS e uma função em Python que facilita a comunicação entre os dois (deixe o servidor python rodando de fundo e chame as funçoes dele <img src='http://armandokeller.com/blog/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  )</p>
<p>Primeiro a classe Python.as que pode ser baixada <a href="http://cid-cd79b3b43841b5ae.skydrive.live.com/self.aspx/.Public/Python.as">aqui</a> :</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #9900cc; font-weight: bold;">package</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #0033ff; font-weight: bold;">import</span> com<span style="color: #000066; font-weight: bold;">.</span>adobe<span style="color: #000066; font-weight: bold;">.</span>serialization<span style="color: #000066; font-weight: bold;">.</span>json<span style="color: #000066; font-weight: bold;">.</span>JSON<span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #0033ff; font-weight: bold;">import</span> <span style="color: #004993;">flash.net</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">Socket</span><span style="color: #000066; font-weight: bold;">;</span>
   <span style="color: #3f5fbf;">/**
     * Classe para conexao com python utilizando socket
     * @author    Armando Leopoldo Keller - (http://alkeller.wordpress.com) - alkeller@live.de
     */</span>
    <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #9900cc; font-weight: bold;">class</span> Python <span style="color: #0033ff; font-weight: bold;">extends</span> <span style="color: #004993;">Socket</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #009900; font-style: italic;">// TODO: Implementar um metodo de retorno, verificando o tipo do retorno, e passando como parametro na funcao associada</span>
        <span style="color: #3f5fbf;">/**
         * Cria um novo Socket
         * @param host    Host onde esta o socket
         * @param port    Porta que esta o socket
         * @see flash.net.Socket
         * @usage    var p:Python = new Python(&quot;10.1.1.23&quot;,2727);
         */</span>
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> Python<span style="color: #000000;">&#40;</span>host<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span>=<span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> port<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">uint</span>=<span style="color: #000000; font-weight:bold;">0</span><span style="color: #000000;">&#41;</span>        <span style="color: #000000;">&#123;</span>
            <span style="color: #0033ff; font-weight: bold;">super</span><span style="color: #000000;">&#40;</span>host<span style="color: #000066; font-weight: bold;">,</span> port<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            host = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">;</span>
            port = <span style="color: #000000; font-weight:bold;">0</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #000000;">&#125;</span>
        <span style="color: #3f5fbf;">/**
         * Chama uma funcao do python se o socket estiver conectado 
         * @param func    Nome da funcao a ser chamada no python
         * @param args    Argumentos a serem chamados no python
         * @usage p.call(&quot;teste&quot;,{um:1,dois:2,tres:3});
         * @usage p.call(&quot;teste&quot;);
         */</span>
        <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> <span style="color: #004993;">call</span><span style="color: #000000;">&#40;</span>func<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">,</span>args<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span>=<span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
        <span style="color: #000000;">&#123;</span>
            <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">connected</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
                <span style="color: #6699cc; font-weight: bold;">var</span> obj<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Object</span> = <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Object</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
                obj<span style="color: #000000;">&#91;</span><span style="color: #990000;">&quot;funcao&quot;</span><span style="color: #000000;">&#93;</span> = func<span style="color: #000066; font-weight: bold;">;</span>
                <span style="color: #0033ff; font-weight: bold;">for</span><span style="color: #000000;">&#40;</span><span style="color: #6699cc; font-weight: bold;">var</span> i<span style="color: #000066; font-weight: bold;">:*</span> <span style="color: #0033ff; font-weight: bold;">in</span> args<span style="color: #000000;">&#41;</span>
                <span style="color: #000000;">&#123;</span>
                    <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>args<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> <span style="color: #0033ff; font-weight: bold;">is</span> <span style="color: #004993;">String</span><span style="color: #000000;">&#41;</span> args<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span> = <span style="color: #990000;">&quot;'&quot;</span><span style="color: #000066; font-weight: bold;">+</span>args<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">+</span><span style="color: #990000;">&quot;'&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
                    obj<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span>=args<span style="color: #000000;">&#91;</span>i<span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">;</span>
                <span style="color: #000000;">&#125;</span>
                <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">writeUTFBytes</span><span style="color: #000000;">&#40;</span>JSON<span style="color: #000066; font-weight: bold;">.</span>encode<span style="color: #000000;">&#40;</span>obj<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
                obj = <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0033ff; font-weight: bold;">else</span>
                <span style="color: #0033ff; font-weight: bold;">throw</span> <span style="color: #0033ff; font-weight: bold;">new</span> <span style="color: #004993;">Error</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Voce deve estar conectado ao socket.&quot;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
        <span style="color: #000000;">&#125;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Depois é necessario a função em python que vai reconhecer o que está sendo chamado pelo flash, o arquivo pode ser baixado <a href="http://cid-cd79b3b43841b5ae.skydrive.live.com/self.aspx/.Public/JMF.py">aqui</a> :</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> json<span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">def</span> executar<span style="color: black;">&#40;</span>objeto<span style="color: black;">&#41;</span>:
    __arr__ = <span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">;</span>
    <span style="color: #0000cd;">__str__</span> = <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #66cc66;">;</span>
    obj = json.<span style="color: black;">loads</span><span style="color: black;">&#40;</span>objeto<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
    <span style="color: #0000cd;">__str__</span> = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>obj<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;funcao&quot;</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>+<span style="color: #483d8b;">&quot;(&quot;</span><span style="color: #66cc66;">;</span>
    obj.<span style="color: black;">pop</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;funcao&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
    <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">len</span><span style="color: black;">&#40;</span>obj.<span style="color: black;">keys</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;;</span>= <span style="color: #ff4500;">1</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> k <span style="color: #ff7700;font-weight:bold;">in</span> obj:
            __arr__.<span style="color: black;">append</span><span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>k<span style="color: black;">&#41;</span>+<span style="color: #483d8b;">&quot;=&quot;</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>obj<span style="color: black;">&#91;</span>k<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>__arr__<span style="color: black;">&#41;</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
            <span style="color: #0000cd;">__str__</span> = <span style="color: #0000cd;">__str__</span>+__arr__<span style="color: black;">&#91;</span>x<span style="color: black;">&#93;</span>+<span style="color: #483d8b;">&quot;,&quot;</span><span style="color: #66cc66;">;</span>
        <span style="color: #0000cd;">__str__</span> = <span style="color: #0000cd;">__str__</span> +__arr__<span style="color: black;">&#91;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>__arr__<span style="color: black;">&#41;</span>-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>+<span style="color: #483d8b;">&quot;)&quot;</span><span style="color: #66cc66;">;</span>
    <span style="color: #ff7700;font-weight:bold;">else</span>:
        <span style="color: #0000cd;">__str__</span> = <span style="color: #0000cd;">__str__</span>+<span style="color: #483d8b;">&quot;)&quot;</span><span style="color: #66cc66;">;</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #0000cd;">__str__</span><span style="color: #66cc66;">;</span></pre></td></tr></table></div>

<p>Agora precisamos de um “servidor socket” rodando de fundo com as funçoes a serem chamadas, que pode ser baixado <a href="http://cid-cd79b3b43841b5ae.skydrive.live.com/self.aspx/.Public/ServidorPython.py">aqui</a> :</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">socket</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">threading</span>
<span style="color: #ff7700;font-weight:bold;">import</span> json
<span style="color: #ff7700;font-weight:bold;">from</span> JMF <span style="color: #ff7700;font-weight:bold;">import</span> executar
<span style="color: #ff7700;font-weight:bold;">class</span> ClientThread <span style="color: black;">&#40;</span> <span style="color: #dc143c;">threading</span>.<span style="color: black;">Thread</span> <span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span> <span style="color: black;">&#40;</span> <span style="color: #008000;">self</span>, channel, details <span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">channel</span> = channel
        <span style="color: #008000;">self</span>.<span style="color: black;">details</span> = details
        <span style="color: #dc143c;">threading</span>.<span style="color: black;">Thread</span>.<span style="color: #0000cd;">__init__</span> <span style="color: black;">&#40;</span> <span style="color: #008000;">self</span> <span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> run <span style="color: black;">&#40;</span> <span style="color: #008000;">self</span> <span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Conexao recebida: '</span>, <span style="color: #008000;">self</span>.<span style="color: black;">details</span> <span style="color: black;">&#91;</span> <span style="color: #ff4500;">0</span> <span style="color: black;">&#93;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">channel</span>.<span style="color: black;">send</span> <span style="color: black;">&#40;</span> <span style="color: #483d8b;">&quot;resposta vinda do python&quot;</span> <span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> x <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">xrange</span> <span style="color: black;">&#40;</span> <span style="color: #ff4500;">10</span> <span style="color: black;">&#41;</span>:
            <span style="color: #dc143c;">string</span> = <span style="color: #008000;">self</span>.<span style="color: black;">channel</span>.<span style="color: black;">recv</span> <span style="color: black;">&#40;</span> <span style="color: #ff4500;">1024</span> <span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">interpretaComando</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">string</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">channel</span>.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">'Conexao fechada: '</span>, <span style="color: #008000;">self</span>.<span style="color: black;">details</span> <span style="color: black;">&#91;</span> <span style="color: #ff4500;">0</span> <span style="color: black;">&#93;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> interpretaComando<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,<span style="color: #dc143c;">string</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">eval</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;self.&quot;</span>+executar<span style="color: black;">&#40;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">string</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> testar<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,arg1,arg2<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Chamou testar: &quot;</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>arg1<span style="color: black;">&#41;</span>+<span style="color: #483d8b;">&quot;,&quot;</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>arg2<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">channel</span>.<span style="color: black;">send</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Chamou testar: &quot;</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>arg1<span style="color: black;">&#41;</span>+<span style="color: #483d8b;">&quot;,&quot;</span>+<span style="color: #008000;">str</span><span style="color: black;">&#40;</span>arg2<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> pegaVariaveis<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;chamou pegaVariaveis&quot;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">channel</span>.<span style="color: black;">send</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;chamou pegaVariaveis&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> pegarArray<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Chamou pegar Array&quot;</span><span style="color: #66cc66;">;</span>
        arr = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'a'</span>,<span style="color: #483d8b;">'b'</span>,<span style="color: #483d8b;">'c'</span>,<span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">2</span>,<span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">;</span>
        txt = json.<span style="color: black;">dumps</span><span style="color: black;">&#40;</span>arr<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">channel</span>.<span style="color: black;">send</span><span style="color: black;">&#40;</span>txt<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> pegarObj<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Chamou pegar Objeto&quot;</span><span style="color: #66cc66;">;</span>
        teste = <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>
        teste<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;item1&quot;</span><span style="color: black;">&#93;</span>   =   <span style="color: #483d8b;">&quot;Primeiro item&quot;</span><span style="color: #66cc66;">;</span>
        teste<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;item2&quot;</span><span style="color: black;">&#93;</span>   =   <span style="color: #483d8b;">&quot;Segundo item&quot;</span><span style="color: #66cc66;">;</span>
        teste<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;numero&quot;</span><span style="color: black;">&#93;</span>  =   <span style="color: #ff4500;">2</span><span style="color: #66cc66;">;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">channel</span>.<span style="color: black;">send</span><span style="color: black;">&#40;</span>json.<span style="color: black;">dumps</span><span style="color: black;">&#40;</span>teste<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #808080; font-style: italic;"># Inicia o servidor</span>
server = <span style="color: #dc143c;">socket</span>.<span style="color: #dc143c;">socket</span> <span style="color: black;">&#40;</span> <span style="color: #dc143c;">socket</span>.<span style="color: black;">AF_INET</span>, <span style="color: #dc143c;">socket</span>.<span style="color: black;">SOCK_STREAM</span> <span style="color: black;">&#41;</span>
server.<span style="color: black;">bind</span> <span style="color: black;">&#40;</span> <span style="color: black;">&#40;</span> <span style="color: #dc143c;">socket</span>.<span style="color: black;">gethostbyname</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">socket</span>.<span style="color: black;">gethostname</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>, <span style="color: #ff4500;">2727</span> <span style="color: black;">&#41;</span> <span style="color: black;">&#41;</span>
server.<span style="color: black;">listen</span> <span style="color: black;">&#40;</span> <span style="color: #ff4500;">5</span> <span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #483d8b;">&quot;Iniciando servidor...&quot;</span>
<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
    channel, details = server.<span style="color: black;">accept</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    ClientThread <span style="color: black;">&#40;</span> channel, details<span style="color: black;">&#41;</span>.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Pronto, agora só falta uma aplicação de exemplo chamando estas funçoes, que pode ser baixada <a href="http://cid-cd79b3b43841b5ae.skydrive.live.com/self.aspx/.Public/PyFlex.mxml">aqui</a> :</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">&lt;?</span>xml <span style="color: #004993;">version</span>=<span style="color: #990000;">&quot;1.0&quot;</span> encoding=<span style="color: #990000;">&quot;utf-8&quot;</span><span style="color: #000066; font-weight: bold;">?&gt;;</span>
<span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>WindowedApplication applicationComplete=<span style="color: #990000;">&quot;iniciar()&quot;</span> xmlns<span style="color: #000066; font-weight: bold;">:</span>mx=<span style="color: #990000;">&quot;http://www.adobe.com/2006/mxml&quot;</span> layout=<span style="color: #990000;">&quot;absolute&quot;</span><span style="color: #000066; font-weight: bold;">&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>Script<span style="color: #000066; font-weight: bold;">&gt;</span>
        <span style="color: #000066; font-weight: bold;">&lt;!</span><span style="color: #000000;">&#91;</span>CDATA<span style="color: #000000;">&#91;</span>
            <span style="color: #0033ff; font-weight: bold;">import</span> com<span style="color: #000066; font-weight: bold;">.</span>adobe<span style="color: #000066; font-weight: bold;">.</span>serialization<span style="color: #000066; font-weight: bold;">.</span>json<span style="color: #000066; font-weight: bold;">.</span>JSON<span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #6699cc; font-weight: bold;">var</span> py<span style="color: #000066; font-weight: bold;">:</span>Python
            <span style="color: #000000;">&#91;</span>Bindable<span style="color: #000000;">&#93;</span>
            <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> texto<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;&quot;</span>
            <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> iniciar<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
            <span style="color: #000000;">&#123;</span>
                py = <span style="color: #0033ff; font-weight: bold;">new</span> Python<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
                py<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">ProgressEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">SOCKET_DATA</span><span style="color: #000066; font-weight: bold;">,</span>onRecebe<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
                py<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">Event</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">CONNECT</span><span style="color: #000066; font-weight: bold;">,</span>onConecta<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
                py<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">addEventListener</span><span style="color: #000000;">&#40;</span><span style="color: #004993;">IOErrorEvent</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">IO_ERROR</span><span style="color: #000066; font-weight: bold;">,</span>onErroIO<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
                py<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">connect</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;10.1.1.3&quot;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000; font-weight:bold;">2727</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> onRecebe<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">ProgressEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #6699cc; font-weight: bold;">var</span> str<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = py<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">readUTFBytes</span><span style="color: #000000;">&#40;</span>py<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">bytesAvailable</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
                texto = str<span style="color: #000066; font-weight: bold;">;</span>
                py<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">flush</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> onConecta<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">Event</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Conectou&quot;</span><span style="color: #000000;">&#41;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> chamarTeste<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>py<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">connected</span><span style="color: #000000;">&#41;</span>
                    py<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">call</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;testar&quot;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #000000;">&#123;</span>arg1<span style="color: #000066; font-weight: bold;">:</span><span style="color: #990000;">&quot;Argumento 1&quot;</span><span style="color: #000066; font-weight: bold;">,</span>arg2<span style="color: #000066; font-weight: bold;">:</span><span style="color: #990000;">&quot;Argumento 2&quot;</span><span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0033ff; font-weight: bold;">else</span>
                    texto = <span style="color: #990000;">&quot;O socket esta desconectado&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> chamarPegaVariaveis<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>py<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">connected</span><span style="color: #000000;">&#41;</span>
                    py<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">call</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;pegaVariaveis&quot;</span><span style="color: #000000;">&#41;</span>
                <span style="color: #0033ff; font-weight: bold;">else</span>
                    texto = <span style="color: #990000;">&quot;O socket esta desconectado&quot;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> pegarArray<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>py<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">connected</span><span style="color: #000000;">&#41;</span>
                    py<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">call</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;pegarArray&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
                <span style="color: #0033ff; font-weight: bold;">else</span>
                    texto = <span style="color: #990000;">&quot;O socket esta desconectado&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> pegarObj<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #0033ff; font-weight: bold;">if</span><span style="color: #000000;">&#40;</span>py<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">connected</span><span style="color: #000000;">&#41;</span>
                    py<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">call</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;pegarObj&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
                <span style="color: #0033ff; font-weight: bold;">else</span>
                    texto = <span style="color: #990000;">&quot;O socket esta desconectado&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0033ff; font-weight: bold;">private</span> <span style="color: #339966; font-weight: bold;">function</span> matarServer<span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
            <span style="color: #000000;">&#123;</span>
                py<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
                texto = <span style="color: #990000;">&quot;Desconectado&quot;</span>
            <span style="color: #000000;">&#125;</span>
            <span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> onErroIO<span style="color: #000000;">&#40;</span>e<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">IOErrorEvent</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #0033ff; font-weight: bold;">void</span>
            <span style="color: #000000;">&#123;</span>
                <span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;Erro ao conectar com o socket&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
            <span style="color: #000000;">&#125;</span>
        <span style="color: #000000;">&#93;</span><span style="color: #000000;">&#93;</span><span style="color: #000066; font-weight: bold;">&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;/</span>mx<span style="color: #000066; font-weight: bold;">:</span>Script<span style="color: #000066; font-weight: bold;">&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>VBox<span style="color: #000066; font-weight: bold;">&gt;</span>
        <span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>HBox<span style="color: #000066; font-weight: bold;">&gt;</span>
            <span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>Button label = <span style="color: #990000;">&quot;Testar&quot;</span>         <span style="color: #004993;">click</span> = <span style="color: #990000;">&quot;chamarTeste()&quot;</span>         <span style="color: #000066; font-weight: bold;">/&gt;</span>
            <span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>Button label = <span style="color: #990000;">&quot;PegaVar&quot;</span>        <span style="color: #004993;">click</span> = <span style="color: #990000;">&quot;chamarPegaVariaveis()&quot;</span> <span style="color: #000066; font-weight: bold;">/&gt;</span>
            <span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>Button label = <span style="color: #990000;">&quot;pegarArray&quot;</span>     <span style="color: #004993;">click</span> = <span style="color: #990000;">&quot;pegarArray()&quot;</span>          <span style="color: #000066; font-weight: bold;">/&gt;</span>
            <span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>Button label = <span style="color: #990000;">&quot;pegarObj&quot;</span>       <span style="color: #004993;">click</span> = <span style="color: #990000;">&quot;pegarObj()&quot;</span>            <span style="color: #000066; font-weight: bold;">/&gt;</span>
            <span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>Button label = <span style="color: #990000;">&quot;Kill Server&quot;</span>    <span style="color: #004993;">click</span> = <span style="color: #990000;">&quot;matarServer()&quot;</span>         <span style="color: #000066; font-weight: bold;">/&gt;</span>
        <span style="color: #000066; font-weight: bold;">&lt;/</span>mx<span style="color: #000066; font-weight: bold;">:</span>HBox<span style="color: #000066; font-weight: bold;">&gt;</span>
        <span style="color: #000066; font-weight: bold;">&lt;</span>mx<span style="color: #000066; font-weight: bold;">:</span>Text <span style="color: #004993;">text</span>=<span style="color: #990000;">&quot;{texto}&quot;</span> <span style="color: #004993;">width</span>=<span style="color: #990000;">&quot;100%&quot;</span> <span style="color: #004993;">height</span>=<span style="color: #990000;">&quot;115&quot;</span><span style="color: #000066; font-weight: bold;">/&gt;</span>
    <span style="color: #000066; font-weight: bold;">&lt;/</span>mx<span style="color: #000066; font-weight: bold;">:</span>VBox<span style="color: #000066; font-weight: bold;">&gt;</span>
<span style="color: #000066; font-weight: bold;">&lt;/</span>mx<span style="color: #000066; font-weight: bold;">:</span>WindowedApplication<span style="color: #000066; font-weight: bold;">&gt;</span></pre></td></tr></table></div>

<p>Claro que esta aplicação ainda está bastante simples, mas o objetivo é demonstrar como pode ser facil conectar o AS3 com Python utilizando socket, para quem não conhece python, vale a pena dar uma estudada, é uma linguagem muito util, poderosa, e simples, que é utilizada em diversos lugares, alguns exemplos bons são nasa e google (precisa mais ? ), agora mistura isso com AS3  (tem algo MUITO funcional e bonito).</p>
<p><strong>Explicação rapida</strong></p>
<p>A sintaxe é muito parecida com a Tweener (classe de tween) que ja é bastante conhecida, então creio que não deve ter muita dificuldade para utilizar esta classe. Sendo que para chamar uma função do python é só chamar     instancia.call(“nome_da_funcao”,{nome_do_argumento:valor_do_argumento…….});</p>
<p>Se alguem tiver alguma duvida, ou sugestão para esta classe ou para futuras classes, deixe um comentario ou envie por email mesmo.</p>
<p>A formatação dos codigos ainda  não está boa, mas esta melhorando (plugin do Windows Live Writer)  se alguem conhecer algum plugin para formatar codigos que tenha suporte a AS, Python, PHP,e C  me avisa.</p>
]]></content:encoded>
			<wfw:commentRss>http://armandokeller.com/blog/2009/01/conexo-as3-com-python-via-socket-simplificado/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>AMFPHPconn Classe de conexão com AMFPHP</title>
		<link>http://armandokeller.com/blog/2007/12/amfphpconn-classe-de-conexao-com-amfphp/</link>
		<comments>http://armandokeller.com/blog/2007/12/amfphpconn-classe-de-conexao-com-amfphp/#comments</comments>
		<pubDate>Fri, 28 Dec 2007 17:04:59 +0000</pubDate>
		<dc:creator>Armando Leopoldo Keller</dc:creator>
				<category><![CDATA[AMFPHP]]></category>
		<category><![CDATA[AS 2.0]]></category>
		<category><![CDATA[Actionscript 2]]></category>
		<category><![CDATA[AMFPHPconn]]></category>
		<category><![CDATA[classe]]></category>
		<category><![CDATA[conexao]]></category>
		<category><![CDATA[flash]]></category>

		<guid isPermaLink="false">http://alkeller.wordpress.com/2007/12/28/amfphpconn-classe-de-conexao-com-amfphp/</guid>
		<description><![CDATA[Oi, AMFPHPconn é uma classe desenvolvida por mim para facilitar a conexão com o AMFPHP segue abaixo a classe:
// AMFPHPconn.as
import mx.remoting.Service;
import mx.services.Log;
class AMFPHPconn {
public var gateway:String;
public var log:Log;
public var servico:String;
public var Servico:Service;
public function AMFPHPconn (_gateway, _servico, _log) {
this.gateway = _gateway;
this.servico = _servico;
this.log = _log;
this.Servico = new Service(this.gateway, this.log, this.servico, null, null);
}
}

Para utilizar essa classe é só colocar o arquivo AMFPHPconn.as na mesma pasta do SWF.

um exemplo de como utilizar:

// importador.as
import mx.remoting.Service;
import mx.services.Log;
import mx.rpc.RelayResponder;
import mx.rpc.FaultEvent;
import mx.rpc.ResultEvent;
import mx.remoting.PendingCall;
import mx.remoting.RecordSet;

// no flash:

#include "importador.as"
var conn:AMFPHPconn = new AMFPHPconn("http://localhost/amfphp/gateway.php","Login");
// é opcional colocar log
//com log ficaria:
//var conn:AMFPHPconn = new AMFPHPconn("http://localhost/amfphp/gateway.php","Login",new log());
var pc_loga:PendingCall = conn.Servico.loga("armando","keller");
pc_loga.responder = new RelayResponder(this,"rpc_loga",null);
function rpc_loga(re:ResultEvent){
trace("retornou "+re.result);
}]]></description>
			<content:encoded><![CDATA[<div class="socialize-in-content" style="float:left;"><div class="socialize-in-button socialize-in-button-vertical"><script type="text/javascript">
			<!-- 
				tweetmeme_url = "http://armandokeller.com/blog/2007/12/amfphpconn-classe-de-conexao-com-amfphp/";
				tweetmeme_source = "tweetmeme";
				tweetmeme_style = "";
				
			//-->
			</script>
                        <script type="text/javascript" src="http://tweetmeme.com/i/scripts/button.js"></script></div><div class="socialize-in-button socialize-in-button-vertical"><script>
			<!-- 
			var fbShare = {
				url: "http://armandokeller.com/blog/2007/12/amfphpconn-classe-de-conexao-com-amfphp/",
				size: "large",
				google_analytics: "true"
			}
			//-->
			</script>
                        <script src="http://widgets.fbshare.me/files/fbshare.js"></script></div><div class="socialize-in-button socialize-in-button-vertical"><script type="text/javascript"><!-- 
		(function() {
		var s = document.createElement('SCRIPT'), s1 = document.getElementsByTagName('SCRIPT')[0];
		s.type = 'text/javascript';
		s.async = true;
		s.src = 'http://widgets.digg.com/buttons.js';
		s1.parentNode.insertBefore(s, s1);
		})();
		//-->
		</script><a class="DiggThisButton " href="http://digg.com/submit?url=http%3A%2F%2Farmandokeller.com%2Fblog%2F2007%2F12%2Famfphpconn-classe-de-conexao-com-amfphp%2F"></a></div><div class="socialize-in-button socialize-in-button-vertical"><div class="delicious-button"><div class="del-top"><span id="5">0</span>saves</div><div class="del-bot"><a href="http://delicious.com/save" onclick="window.open('http://delicious.com/save?v=5&noui&jump=close&url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title), 'delicious','toolbar=no,width=550,height=550'); return false;">Save</a></div></div>
		<script>
			<!-- 
			function displayURL(data) { var urlinfo = data[0]; if (!urlinfo.total_posts) return;document.getElementById('5').innerHTML = urlinfo.total_posts;}
			//-->
		</script>
		<script src = "http://badges.del.icio.us/feeds/json/url/data?url=http://armandokeller.com/blog/2007/12/amfphpconn-classe-de-conexao-com-amfphp/&amp;callback=displayURL"></script></div></div><p>Oi, AMFPHPconn é uma classe desenvolvida por mim para facilitar a conexão com o <a href="http://www.amfphp.org/" title="AMFPHP site oficial" target="_blank">AMFPHP</a> segue abaixo a classe:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">//  AMFPHPconn.as</span>
<span style="color: #0033ff; font-weight: bold;">import</span> mx<span style="color: #000066; font-weight: bold;">.</span>remoting<span style="color: #000066; font-weight: bold;">.</span>Service<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">import</span> mx<span style="color: #000066; font-weight: bold;">.</span>services<span style="color: #000066; font-weight: bold;">.</span>Log<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #9900cc; font-weight: bold;">class</span> AMFPHPconn
<span style="color: #000000;">&#123;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> gateway<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">log</span><span style="color: #000066; font-weight: bold;">:</span>Log<span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> servico<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #6699cc; font-weight: bold;">var</span> Servico<span style="color: #000066; font-weight: bold;">:</span>Service<span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
	<span style="color: #0033ff; font-weight: bold;">public</span> <span style="color: #339966; font-weight: bold;">function</span> AMFPHPconn <span style="color: #000000;">&#40;</span>_gateway<span style="color: #000066; font-weight: bold;">,</span> _servico<span style="color: #000066; font-weight: bold;">,</span> _log<span style="color: #000000;">&#41;</span>
	<span style="color: #000000;">&#123;</span>
		<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>gateway = _gateway<span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>servico = _servico<span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">log</span> = _log<span style="color: #000066; font-weight: bold;">;</span>
		<span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>Servico = <span style="color: #0033ff; font-weight: bold;">new</span> Service<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>gateway<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">log</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">.</span>servico<span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000066; font-weight: bold;">,</span> <span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
	<span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

<p>Para utilizar essa classe é só colocar o arquivo AMFPHPconn.as  na mesma pasta do SWF.</p>
<p>um exemplo de  como utilizar:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">// importador.as</span>
<span style="color: #0033ff; font-weight: bold;">import</span> mx<span style="color: #000066; font-weight: bold;">.</span>remoting<span style="color: #000066; font-weight: bold;">.</span>Service<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">import</span> mx<span style="color: #000066; font-weight: bold;">.</span>services<span style="color: #000066; font-weight: bold;">.</span>Log<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">import</span> mx<span style="color: #000066; font-weight: bold;">.</span>rpc<span style="color: #000066; font-weight: bold;">.</span>RelayResponder<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">import</span> mx<span style="color: #000066; font-weight: bold;">.</span>rpc<span style="color: #000066; font-weight: bold;">.</span>FaultEvent<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">import</span> mx<span style="color: #000066; font-weight: bold;">.</span>rpc<span style="color: #000066; font-weight: bold;">.</span>ResultEvent<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">import</span> mx<span style="color: #000066; font-weight: bold;">.</span>remoting<span style="color: #000066; font-weight: bold;">.</span>PendingCall<span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #0033ff; font-weight: bold;">import</span> mx<span style="color: #000066; font-weight: bold;">.</span>remoting<span style="color: #000066; font-weight: bold;">.</span>RecordSet<span style="color: #000066; font-weight: bold;">;</span></pre></td></tr></table></div>

<p>// no flash:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="actionscript3" style="font-family:monospace;">#include <span style="color: #990000;">&quot;importador.as&quot;</span>
<span style="color: #6699cc; font-weight: bold;">var</span> conn<span style="color: #000066; font-weight: bold;">:</span>AMFPHPconn = <span style="color: #0033ff; font-weight: bold;">new</span> AMFPHPconn<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;http://localhost/amfphp/gateway.php&quot;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #990000;">&quot;Login&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #009900; font-style: italic;">// é opcional colocar log</span>
<span style="color: #009900; font-style: italic;">//com log ficaria:</span>
<span style="color: #009900; font-style: italic;">//var conn:AMFPHPconn = new AMFPHPconn(&quot;http://localhost/amfphp/gateway.php&quot;,&quot;Login&quot;,new log());</span>
<span style="color: #6699cc; font-weight: bold;">var</span> pc_loga<span style="color: #000066; font-weight: bold;">:</span>PendingCall = conn<span style="color: #000066; font-weight: bold;">.</span>Servico<span style="color: #000066; font-weight: bold;">.</span>loga<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;armando&quot;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #990000;">&quot;keller&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
pc_loga<span style="color: #000066; font-weight: bold;">.</span>responder = <span style="color: #0033ff; font-weight: bold;">new</span> RelayResponder<span style="color: #000000;">&#40;</span><span style="color: #0033ff; font-weight: bold;">this</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #990000;">&quot;rpc_loga&quot;</span><span style="color: #000066; font-weight: bold;">,</span><span style="color: #0033ff; font-weight: bold;">null</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #339966; font-weight: bold;">function</span> rpc_loga<span style="color: #000000;">&#40;</span>re<span style="color: #000066; font-weight: bold;">:</span>ResultEvent<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#123;</span>
	<span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;retornou &quot;</span><span style="color: #000066; font-weight: bold;">+</span>re<span style="color: #000066; font-weight: bold;">.</span>result<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://armandokeller.com/blog/2007/12/amfphpconn-classe-de-conexao-com-amfphp/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

