Connect to Socket server from Actionscript 3 AIR application
I read through the method to connect to a socket server : http://livedocs.adobe.com/flex/3/html/help.html?content=17_Networking_and_communications_5.html
I wrote an AIR application to connect to the java server code, but the client cannot connect. What is missing in the code?
<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe开发者_StackOverflow社区.com/mxml/2009"
xmlns:s="library://ns.adobe.com/flex/spark"
xmlns:mx="library://ns.adobe.com/flex/mx" backgroundColor="#D5F8C3">
<fx:Script>
<![CDATA[
import flash.net.XMLSocket;
var socket:XMLSocket;
public function b1_clickHandler(event:MouseEvent):void
{
var socket:XMLSocket;
socket.connect("127.0.0.1",8080);
socket.send("Hello");
//socket.addEventListener(DataEvent.DATA, onData);
}
]]>
</fx:Script>
<fx:Declarations>
<!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>
<s:Button id="b1" x="285" y="162" label="Send Text" width="201" height="105" click="b1_clickHandler(event)"/>
</s:WindowedApplication>
You have to wait for CONNECTED event before sending data. Also, be sure you've managed crossdomain security (look at : http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7c60.html#WS5b3ccc516d4fbf351e63e3d118a9b90204-7c63)
Server/client code exemple : Air 2 ServerSocket Crossdomain problem
精彩评论