tomfmason Posted November 17, 2008 Share Posted November 17, 2008 I got it working and I was indeed correct about the policy request. I noticed the following things. When setting the host for the xml socket from flash it must be connecting to the same host that the socket server is listening on e,g, localhost. The second is that you need to respond correctly to the policy request from flash. Here is the working code and the action script I used to test it(complied with mtasc) #!/usr/bin/php -q <?php /* Raymond Fain Used for PHP5 Sockets with Flash 8 Tutorial for Kirupa.com For any questions or concerns, email me at [email]ray@obi-graphics.com[/email] or simply visit the site, [url=http://www.php.net]www.php.net[/url], to see if you can find an answer. */ error_reporting(E_ALL); set_time_limit(0); ob_implicit_flush(); //$address = '192.168.1.75'; $address = 'localhost'; //$address = '127.0.0.1'; $port = 45063; $policyFile = '<cross-domain-policy><allow-access-from domain="*" to-ports="45063" /></cross-domain-policy>'; $policyRequest = '<policy-file-request/>'; //---- Function to Send out Messages to Everyone Connected ---------------------------------------- function send_Message($allclient, $socket, $buf) { foreach($allclient as $client) { socket_write($client, "$client talk: $buf"); } } //---- Start Socket creation for PHP 5 Socket Server ------------------------------------- if (($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) { echo "socket_create() failed, reason: " . socket_strerror($master) . " "; } socket_set_option($master, SOL_SOCKET,SO_REUSEADDR, 1); if (($ret = socket_bind($master, $address, $port)) < 0) { echo "socket_bind() failed, reason: " . socket_strerror($ret) . " "; } if (($ret = socket_listen($master, 5)) < 0) { echo "socket_listen() failed, reason: " . socket_strerror($ret) . " "; } $read_sockets = array($master); //---- Create Persistent Loop to continuously handle incoming socket messages --------------------- while (true) { $changed_sockets = $read_sockets; $num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL); foreach($changed_sockets as $socket) { if ($socket == $master) { if (($client = socket_accept($master)) < 0) { echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . " "; continue; } else { array_push($read_sockets, $client); } } else { $bytes = socket_recv($socket, $buffer, 2048, 0); if ($bytes == 0) { $index = array_search($socket, $read_sockets); unset($read_sockets[$index]); socket_shutdown($socket,2); socket_close($socket); }else{ $allclients = $read_sockets; array_shift($allclients); if($buffer == $policyRequest . "\0"){ print "received policy request and sending the policy file \n"; socket_write($client, $policyFile); } else { send_Message($allclients, $socket, $buffer); } } } } } ?> /* Compile using MTACS (http://www.mtasc.org/) mtasc -version 8 -header 1:1:1 -main -swf test.swf test.as */ import flash.external.ExternalInterface; class SocketServer { static var socket : XMLSocket; static var host:String = 'localhost'; static var port:Number = 45063; static var test:String; static function connect() { // Create new XMLSocket object System.security.loadPolicyFile('xmlsocket://' + host + ':' + port); log('Attempting to connect to' + host + ' on port ' + port); socket = new XMLSocket(); socket.connect(host, port); socket.onXML = newXML; socket.onConnect = onConnect; socket.onClose = onDisconnect; } static function log(data:String) { ExternalInterface.call("alert", "flash method log called") ExternalInterface.call('log', data); } static function onConnect(success:Boolean) { if (success) { log('Connected to ' + host + ' on port ' + port); log('connected'); } else { log('Error connecting to ' + host + ' on port ' + port); } } static function onDisconnect() { log('Diconnected from ' + host + ' on port ' + port); } static function sendData(data:String){ socket.send(data) } static function newXML(input:XML) { ExternalInterface.call('receiveData',input.toString()); } static function main() { ExternalInterface.addCallback("log", null, log); ExternalInterface.call("alert", "loaded flash flile"); connect(); } } Hope that works for you. Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/page/2/#findComment-691853 Share on other sites More sharing options...
The Little Guy Posted November 17, 2008 Author Share Posted November 17, 2008 What version of AS is that (1, 2 or 3)? Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/page/2/#findComment-691857 Share on other sites More sharing options...
tomfmason Posted November 17, 2008 Share Posted November 17, 2008 What version of AS is that (1, 2 or 3)? 2 but that shouldn't matter. AS3 would require the same thing. Just use the same host in your socket connection from flash as the server is listening on and it should automagically work regardless of the AS version Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/page/2/#findComment-691858 Share on other sites More sharing options...
The Little Guy Posted November 17, 2008 Author Share Posted November 17, 2008 Whe the output is sent back, it doesn't go into my onXML function, and I don't know why. import flash.external.ExternalInterface; var host = 'localhost'; //var host = '192.168.1.75'; //var host = '127.0.0.1'; var port = 45063; msgArea.htmlText = "Host: "+host; msgArea.htmlText += "Port: "+port; System.security.loadPolicyFile('xmlsocket://'+host+':'+port); mySocket = new XMLSocket(); mySocket.connect(host, port); mySocket.onXML = newXML; function newXML(input:XML){ ExternalInterface.call('receiveData',input.toString()); msgArea.htmlText += 'here'; } Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/page/2/#findComment-691877 Share on other sites More sharing options...
The Little Guy Posted November 18, 2008 Author Share Posted November 18, 2008 I think I have a problem, it says: Ignoring policy file with incorrect syntax: xmlsocket://localhost:45063 Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/page/2/#findComment-692429 Share on other sites More sharing options...
DarkWater Posted November 18, 2008 Share Posted November 18, 2008 Apache is listening on port 6666 is correct The PHP: #!/usr/bin/php -q <?php /* Raymond Fain Used for PHP5 Sockets with Flash 8 Tutorial for Kirupa.com For any questions or concerns, email me at ray@obi-graphics.com or simply visit the site, www.php.net, to see if you can find an answer. */ error_reporting(E_ALL); set_time_limit(0); ob_implicit_flush(); //$address = '192.168.1.75'; $address = 'localhost'; $port = 1213; //---- Function to Send out Messages to Everyone Connected ---------------------------------------- function send_Message($allclient, $socket, $buf) { foreach($allclient as $client) { socket_write($client, "$client talk: $buf"); } } //---- Start Socket creation for PHP 5 Socket Server ------------------------------------- if (($master = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) < 0) { echo "socket_create() failed, reason: " . socket_strerror($master) . " "; } socket_set_option($master, SOL_SOCKET,SO_REUSEADDR, 1); if (($ret = socket_bind($master, $address, $port)) < 0) { echo "socket_bind() failed, reason: " . socket_strerror($ret) . " "; } if (($ret = socket_listen($master, 5)) < 0) { echo "socket_listen() failed, reason: " . socket_strerror($ret) . " "; } $read_sockets = array($master); //---- Create Persistent Loop to continuously handle incoming socket messages --------------------- while (true) { $changed_sockets = $read_sockets; $num_changed_sockets = socket_select($changed_sockets, $write = NULL, $except = NULL, NULL); foreach($changed_sockets as $socket) { if ($socket == $master) { if (($client = socket_accept($master)) < 0) { echo "socket_accept() failed: reason: " . socket_strerror($msgsock) . " "; continue; } else { array_push($read_sockets, $client); } } else { $bytes = socket_recv($socket, $buffer, 2048, 0); if ($bytes == 0) { $index = array_search($socket, $read_sockets); unset($read_sockets[$index]); socket_shutdown($socket,2); socket_close($socket); }else{ $allclients = $read_sockets; array_shift($allclients); send_Message($allclients, $socket, $buffer); } } } } ?> The Flash: mySocket = new XMLSocket(); mySocket.onConnect = function(success) { if (success) { msgArea.htmlText += "<b>Server connection established!</b>"; } else { msgArea.htmlText += "<b>Server connection failed!</b>"; } } mySocket.onClose = function() { msgArea.htmlText += "<b>Server connection lost</b>"; } XMLSocket.prototype.onData = function(msg) { msgArea.htmlText += msg; } mySocket.connect('localhost', 1213); //--- Handle button click -------------------------------------- function msgGO() { if (inputMsg.htmlText != "") { mySocket.send(inputMsg.htmlText); inputMsg.htmlText = ""; } } pushMsg.onRelease = function() { msgGO(); } keyListener = new Object(); keyListener.onKeyDown = function(){ xk = parseInt(Key.getAscii().toString()); if(xk == 13){ msgGO(); } } Key.addListener(keyListener); The tutorial can be found here: http://www.kirupa.com/developer/flash8/php5sockets_flash8.htm One thing I noticed, with a quick glance, was that you were using array_push(). Why? It's much slower. =P Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/page/2/#findComment-692433 Share on other sites More sharing options...
The Little Guy Posted November 18, 2008 Author Share Posted November 18, 2008 Its just what was in the tutorial. Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/page/2/#findComment-692455 Share on other sites More sharing options...
The Little Guy Posted November 20, 2008 Author Share Posted November 20, 2008 I GOT IT TO CONNECT! I needed to close the socket. I guess it reopens a new socket and it has the policy file! if($buffer == $policyRequest . "\0"){ socket_write($client, $policyFile); $index = array_search($socket, $read_sockets); unset($read_sockets[$index]); socket_shutdown($socket, 2); socket_close($socket); }else{ send_Message($allclients, $socket, $buffer); } Quote Link to comment https://forums.phpfreaks.com/topic/132793-flash-connecect/page/2/#findComment-694026 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.