Simple Socket System
Following is a very simple demonstration program that will send a fixed-length string to a server and receive the same string echoed back. We create three XMLSocket callback functions to handle onOpen, onClose, and onXML events. A fourth function is attached to a submit button. This submit() function packages the input text into XML and transmits it to the server using XMLSocket.send().
ActionScript
socko= new XMLSocket();
Since this is a simple exercise, we make and modify a single instance of XMLSocket. A more ambitious project might extend the XMLSocket object by creating new prototype functionality.
ActionScript
socko.obj = this._target;
The methods we create need to address the namespace of the MovieClip which is creating this socket. We create text entry and display fields that manage the strings we are sending and receiving. The onscreen text fields and the XMLSocket routines communicate through variables in the clip's namespace. The obj property is our own device. It allows us to easily access these MovieClip variables as, for example, obj.status or this.obj.textOut. The namespace is inserted into the XMLSocket object as its obj property.
| portno
|
target port, set by default but overwritable for convenience
|
| status
|
socket communications status report
|
| textIn
|
latest message from the server
|
| textOut
|
line being composed for uploading
|
The first frame of the socko MovieClip has a single simple line of code.
ActionScript
status ="";
All it does is clear the status line, so if we make repeated connection attempts, it removes the previous result and stays neat. (We would make a second attempt by typing in a new value for portno and clicking the button near it, which calls gotoAndPlay(1);. This, in turn, will execute this code and clear the status display.

Frame 2 has this bit of active code.
ActionScript
if (!portno) portno= 12538;
status += socko.connect( "", portno )? " [Connect":" [Connect FAILS ] ";
stop();
It sets a default for the port, if the user has not, and it attempts a connection. Note that it connects to "", which means the server from which it was downloaded. The only way this will fail initially (and give the FAILS message) is if the port is below 1024.
But we will code like this, hoping that ActionScript will continue to become more sophisticated and that this kind of test will be more useful.
ActionScript
socko.onConnect = function ( ok ){
status+= ok? "ed] ":"ion Not Completed] ";
}
This code completes a phrase begun when the XMLSocket attempted to launch a connection. When it began to contact the server, it printed on the screen's status line: "[Connect". Now, depending on the outcome, it becomes "[Connected]" or "[Connection not Completed]".
It is good that this error message is different from the message we print in the case that connect() itself returns a failure code.
ActionScript
socko.onClose = function(){
status+= " [Disconnected by server] ";
}
This message is an important status message. It comes when the connection is severed by the server, probably at a time when the user does not expect it. (A nice improvement here would be the addition of an appropriate alert sound.)
ActionScript
function transmit() {
textOut+=" ";
textOut= "<msg>"+textOut.slice( 0, 36)+"</msg>";
socko.send( new XML(textOut) );
textOut="";
}
This function pads the output line with 36 blanks, then trims the result to a total length of 36. When the tags and the terminating null are added, the data package is exactly 48 characters.
While we generally prefer more flexible code, we need to work within the restrictions of the PHP and ActionScript routines. We found that we saved ourselves an extraordinary amount of pain (and kept this demonstration far cleaner) by restricting our data package to a fixed size.
ActionScript
socko.onXML = function (msg) {
textIn = msg.toString();
}

Rather artlessly, we are going to simply display the incoming XML document in its entirety. This is tolerable, since we know it will be short and simple, but a more mature function would isolate the text from the markup. It would try to display the former formatted by the latter.
All we need now to complete the client is to hook the second button (and the Enter key) to our transmit method. We click on the button instance and add this code to its object actions.
ActionScript
on (release, keyPress "<Enter>") { transmit(); }
|