In the previous
page you setup the Flash animation. Now we need to add in the guts to this flash chat
room. Make sure you have the actions
layer selected and hit F9 to open the Actions panel.
Highlight all of this coding and paste it into the
actions panel.
- 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("dyn.obi-graphics.com",
9999);
-
- //--- Handle button click
--------------------------------------
-
- function
msgGO()
{
- if
(inputMsg.htmlText
!=
"")
{
- mySocket.send(inputMsg.htmlText+"\n");
- inputMsg.htmlText
=
"";
- }
- }
-
- pushMsg.onRelease
=
function()
{
- msgGO();
- };
-
If you tried testing the movie you
should not get a connection. That is because it is
trying to connect to my socket server, but flash is
being denied access. Do not worry about that for now.
Instead, I will go through the ActionScript to explain
what is going on:
- mySocket
=
new XMLSocket();
We are declaring a new object called
mySocket to handle any socket
connections flash might receive or send.
- mySocket.onConnect
=
function(success)
{
- if
(success)
{
- msgArea.htmlText
+=
"<b>Server connection
established!</b>";
- }
else
{
- msgArea.htmlText
+=
"<b>Server connection
failed!</b>";
- }
- };
This will ensure that our
mySocket object will connect to the PHP socket
we will run later. When it receives the big OK, it will
display our first message of "Server connection
established!" but a no-go will give us the failure
message.
- mySocket.onClose
=
function()
{
- msgArea.htmlText
+=
"<b>Server connection
lost</b>";
- };
For whatever reason, if the PHP socket
connection is lost, .onClose will pick
that up and run this function.
- XMLSocket.prototype.onData
=
function(msg)
{
- msgArea.htmlText
+=
msg;
- };
Unlike the other functions where we
could use mySocket, here we can only
use XMLSocket with onData. This
prototype.onData function handles the incoming
packages.
XMLSocket.onData is called every time a
package is sent from the server (or whatever it's
connected to) and can only invoke this event handler
when the package is terminated with a zero byte.
This will be explained in detail later on.
- mySocket.connect("dyn.obi-graphics.com",
9999);
This .connect will try and connect
to dyn.obi-graphics.com on port 9999. Make sure that
your address (or IP) is that of your own.
- function
msgGO()
{
- if
(inputMsg.htmlText
!=
"")
{
- mySocket.send(inputMsg.htmlText+"\n");
- inputMsg.htmlText
=
"";
- }
- }
msgGO() function will handle the
messages people write. It is simply checking to make
sure the inputMsg html area is not
blank. The second part we are using flash's
.send built in function. It will send the
message to our socket script.
- pushMsg.onRelease
=
function()
{
- msgGO();
- };
This is just a function built to call on our
msgGO() function when the 'Send' button is
released.
Go ahead and save the Flash document as
socketClient. You can also go back to
your flash.html page and add in a
.swf version of this. In our
next section we will build our PHP5 socket script to
handle all clients and their messages.
Onwards to the
next page!