views:

1772

answers:

4

My flash game needs to connect to my PHP Socket Server. Because of security things, a policy file has to be send to the flash client when it tries to connect.

The following is what I've done.

In Actionscript / Flex 3 / Flash:

Security.loadPolicyFile("http://[SERVER.IP]:9000/crossdomain.xml");
socket.connect(hostName, port); //connect to the socket
[rest of original code]

To make the socket server respond on the request, I added the following to the server:

elseif (preg_match("/policy-file-request/i", $buffer) or preg_match("/crossdomain/i", $buffer)) {
        socket_write($socket, '<?xml version="1.0"?><cross-domain-policy><site-control permitted-cross-domain-policies="all"/><allow-access-from domain="*" to-ports="9000" /></cross-domain-policy>');
        unset($read_sockets[array_search($socket, $read_sockets)]);
        socket_shutdown($socket, 2);
        socket_close($socket);

I however get the following error: "Ignoring policy file at (URL) due to missing Content-Type." So, I tried to fix this by adding a header right above my xml code:

socket_write($socket, "Content-Type: text/xml\n");

Unfortunately, I still get the same error. Am I giving the content type in a wrong way?

+1  A: 

Try it with \r\n after the Content-Type.

socket_write($socket, "Content-Type: text/xml\r\n");

Shouldn't you be using a xmlsocket on port 843?

Mauricio
I can't use low ports like that (restricted by my webhost).
Tom
Just tried and I still get the same error when I add the \r\n after the Content-Type.
Tom
+2  A: 

You need to return a valid HTTP response if you are going to use:

Security.loadPolicyFile("http://[SERVER.IP]:9000/crossdomain.xml");

If the flash is going to connect to your PHP socket server, just skip the above line and it will try the port itself and expect raw data instead of a HTTP response.

truppo
It doesn't seem to do that. How do I make it return a valid HTTP response?
Tom
+2  A: 

you can load the policyfile from any port of the server using Security.loadPolicyFile() ... maybe you should simply serve it per http on port 80, load it from there and then connect to the server ...

also, by default, i think flashplayer 9 (upwards from some minor version) sends a policyfile request to port 943 by default ... so you might put a server there, to do that ...

a little side note: PHP was never designed for socket servers and is not very good at that ... if you can, try using Java, or NekoVM, that you can use with haXe ... also haXe remoting, as well as ThreadedRemotingServer might be of interest to you ... there's some good and clear tutorials on the haXe site ...

back2dos
I can't use low ports like that (restricted by my webhost).
Tom
+2  A: 

Try sending this:

HTTP/1.1 200 OK\r\nContent-Type: text/xml\r\n\r\n

Make sure nothing is sent before this. Also, send a \r\n before the socket is closed.

Mauricio