Chronicles of Sockets in Flash: the cross-domain, the Sockets & the Ports!

Friday, August 31, 2012

Connecting to a server-side socket from Flash using ActionScript 3.0


Cross domain policy

Its absolutely, undoubtedly not possible to connect to a UDP socket from within Flash (unless of course you are within the application sandbox of an AIR application).

Outside of AIR, Flash can only connect to TCP/IP sockets that too the ones that are specified in the cross-domain xml. This is true even if the swf domain is same as that of the target socket.

Also, the cross domain xml needs to be served by a socket on the same domain (not necessarily the same port as the target socket).

For Flash Player 10 onwards, this xml should come as plain text response over the socket (basically to avoid any HTTP headers).

Moral of the story: UDP not possible (as of now) if you are working on a web-based Flash (non-AIR) application. And, cross-domain policy file needs to be server over a socket & not as an xml file from a web server (even if) on the target domain.

A typical cross-domain xml looks like this.
<?xml version="1.0"?> <!DOCTYPE cross-domain-policy SYSTEM "http://www.macromedia.com/xml/dtds/cross-domain-policy.dtd"> <cross-domain-policy>   <allow-access-from domain="*" secure="false" to-ports="843,1232-1237"> < /cross-domain-policy>

Ensure that you have mentioned the ports that need to access the socket properly in the "to-ports" attribute.
In the above example, the list of all allowed ports are: 843, 1232,1233, 1234, 1235, 1236, and 1237.

Also, if the cross-domain is not working even if it seems that you have done everything right, then try adding a EOL character at the end of the cross-domain string.

Hope this helps!

No comments:

Post a Comment

 

About Me

My photo
Front-end Developer with over 9 years of experience in developing Web 2.0 sites with a keen eye for UX. Avid technology enthusiast; gadget freak; old school PC gamer; loves developing Apps & Data Visualizations.

My Website

Stack Overflow profile

Twitter Profile

Facebook profile