I have a flex .swf and a seperate AIR project which I’m trying to get to communicate with each other via a socket.
The two programs are connecting ok, and the .swf is able to send data across to the AIR application without problem. However, I’m finding that when the AIR app sends data back to Flex, it is never received.
- The .swf is launched locally via a
file://URI in the browser. - The .swf connects, and the
CONNECTevent is fired - I’ve added breakpoints looking for requests for a policy file as suggested here, but I don’t ever see the request for a policy file get issued from the SWF to the AIR app.
Here’s the snippet of code I’m running from the SWF to listen for data:
private var address:String;
private var port:int;
private var socket:Socket;
private var log:ILogger = LogUtil.getLogger(this);
public function SocketPlayer(port:int=41051,address:String="127.0.0.1")
{
this.port = port;
this.address = address;
connect();
}
public function connect():void
{
socket = new Socket();
socket.addEventListener(DataEvent.DATA,onData);
socket.addEventListener(Event.CLOSE,logEvent);
socket.addEventListener(Event.CONNECT,logEvent);
socket.addEventListener(SecurityErrorEvent.SECURITY_ERROR,logEvent);
socket.addEventListener(IOErrorEvent.IO_ERROR,logEvent);
socket.connect(address,port);
log.info("Socket connected on {0}:{1}",address,port);
}
private function logEvent(event:Event):void
{
log.info("Event: {0}", ObjectUtil.toString(event));
if (event is TextEvent)
{
log.error(TextEvent(event).text)
}
}
private function onData(event:DataEvent):void
{
log.info("Data: " + event.data);
}
Here’s the code used in the AIR app to send the data:
public function execute():void
{
var message:String = "Testing...";
socket.writeUTF(message);
socket.flush();
}
(Note that socket is stored locally as passed in the ServerSocketConnectEvent, though this code is not shown here)
If I place a breakpoint in the code that is sending within the SWF, I notice that the socket has a private _timeoutEvent property, which is set to an instance of a flash.events.SecurityErrorEvent with the following error text:
Error #2048: Security sandbox violation: file:///Users/martypitt/dev//ParsleyTest/bin-debug/RecordingTest.swf cannot load data from 127.0.0.1:41051.
However, the SecurityErrorEvent is never caught by my code (I’m not sure if this indicates it was actually ever dispatched).
Are there additional steps I need to take to allow this communication?
Whoops — the issue here is that I’m listening for the wrong event..
I should be listening for
ProgressEvent.SOCKET_DATAinstead ofDataEvent.ON_DATA, as this is aSocket, not anXMLSocket.