xml| Monitoring
4 When the connection is established successfully, the client and the server can send XML data to each other. Send data to the server using the Send method of the Xmlsocket object:
Mysocket.send (myXML);
Where myXML is an XML object that contains XML data, the Send method converts myxml to a string, sends the string to the server, and sends a 0-byte append after the string is sent. The Send method has no return value.
In the following example, Mr. myXML into an empty XML object, and then adds an element node MyLogin in myXML, which contains two properties username and Password,send methods convert myXML to a string and send it to the server. And, of course, append a 0 byte to represent the completion of an XML data:
var myxml = new XML ();
var mylogin = myxml.createelement ("login");
MyLogin.attributes.username = "Morgan";
MyLogin.attributes.password = "Loveme";
Myxml.appendchild (MyLogin);
Mysocket.send (myXML);
When data arrives (a string ending with 0 bytes is received), the OnData event is first triggered, and the corresponding event handler Myondata with a parameter representing the data string that arrived at that time, but does not contain 0 bytes, in the following example, a string "I am Morgan" from the server. Yang! ", plus a 0-byte, the following statement sets the contents of MyTextField (an instance of a non-static literal TextField object) to" I am Morgan! ", and we notice that the data sent here can be any form including XML format:
function Myondata (SRC) {
Mytextfield.text = src;
}
For OnData events, if there are no corresponding event handler functions, the Onxml event is triggered by default, in the following form:
XMLSocket.prototype.onData = function (src) {
This.onxml (New XML (SRC));
}
In the Onxml event, the data from the OnData event is used to generate an XML object and pass the object as a parameter to the handler of the Onxml event, so if you want to customize the processing function of the Onxml event, the data sent from the server must be in XML format. Otherwise, there will be unexpected mistakes. If the handler for the OnData event is set, the handler function for the Onxml event is no longer invoked when the data arrives, unless explicitly invoked, so that in a sense the two events are mutually exclusive.
In the case where the OnData event handler is not set, and the Onxml event handler function is set, the following statement sets the contents of the two Non-static text instances Nametextfield and Passwordtextfield respectively to "Morgan" and "" when the XML data arrives. Loveme ":
function Myonxml (DOC) {
var e = Doc.firstchild;
if (e!= null && e.nodename = = "Login") {
Nametextfield.text = E.attributes.username;
Passwordtextfield.text = E.attributes.password;
}
}
5 Finally, at the end of the program, use the Close method of the Xmlsocket object to turn off the socket connection as follows:
Mysocket.close ();
Note that using the Close method of the Xmlsocket object to turn off the socket connection does not trigger the OnClose event of the Xmlsocket object, triggering the event on the Flash application client only when the socket connection is closed by the server, by default, The OnClose event handler for the Xmlsocket object does not perform any action, and you can customize the event handler to achieve a specific purpose. As in the following statement, the contents of MyTextField (for a non-static text instance) are set to "Socket Closed by Server!" When the OnClose event occurs.
function Myonclose () {
MyTextField. Text = "Socket Closed by Server!";
}