PHP 5 Sockets with Flash 8
       by obiAdmin (raymond): 25 September 2006

In the previous page you learned how to create both a flash client and php server side script to use and handle socket data between each other. You also might have written your first batch file to use PHP shells.

Others
Here I can just give some leads on how to develop a more rich PHP shell script to work with.


Notice how Flash uses XMLsockets to connect and handle socket data? Well XML is growing ever larger everyday, and you can build your flash client and php shell script to handle XML statements. Right now the PHP shell script just passes on any data to everyone it's connected to. Well if you build an XML parser within PHP, you can create attributes that give PHP directions on what to do (like message only certain people, aka private messages, or even set up IRC like commands).

So what steps must you take to advancing this simple yet complex PHP script? Learning more PHP helps, but XML would be the next step. XML gives you the ability to create messages that both PHP and Flash can take apart and understand. Given the example of:

<CHATROOM ACTIONS="private_message" TO="Joe" FROM="Fred" MESSAGE="blah blah" />

You can have the PHP shell script have a generic parser that takes in anything with CHATROOM and checks to see what ACTIONS it may have. Based on that, you can see some other attributes it can contain, making this a little more complex everytime. Now remember, you can have the server itself send messages out to connected sockets, but remember to terminate each message with a zero byte. The best way I found out to do this is something like this:

$errormsg = '<SERVER TEXT="*NOTICE* Please choose a different game title." />' .chr(0x00);

Notice that I have the normal text that I'm sending out to the socket, but I have added a .char(0x00) to the end of the message. This is a message that has been terminated with a zero byte. Now flash will read this message. Without it, flash just ignores the data being sent to it completely.


Debugging Workaround
Now since the PHP shell is running through command prompt, debugging can actually be easy to setup. It will be on your part to come up with some effective methods to achieve this but, by using a PHP command print_r( ), you can see errors or variables that can be displayed in the command prompt. It works just like the normal print( ) command but it only shows up in the command prompt window and has no overall effect on the script.


Summary
Ok so you developed a flash client that connects to a PHP shell script running by command prompt located on an Apache 2 server. WHEW!! That is actually a lot to think about! The flash client uses some simple functions and methods to handle the incoming data and sending data. The PHP shell script handles incoming socket connections, incoming data, and spits out any data it receives to all the sockets connected to it. The PHP shell script also unsets any sockets that have disconnected and closes them out.

You can download this ZIP file that contains the PHP shell script, chat room FLA, and Batch file. It would be recommended that you download this to read up on the comments I have made in the PHP shell script. That way you can get an understanding of how everything works!

Download ZIP

I really hope you have learned a lot from this tutorial! If you have any questions or comments, don't delay. Just ask them in the kirupa.com Forum. After you post your question, either I or some of the forum members will answer it!

Raymond Fain aka ObiAdmin
Arizona State University  [Army ROTC]

 


1 | 2 | 3 | 4 | 5 | 6




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.