Using Flash and .NET Socket Communication to Implement "Push" (2) Flash and Server between Flash and Services: XmlSock

xiaoxiao2021-03-06  63

(2) Communication core between Flash and serve: Xmlsocket object

This part and the next part will be dominated by flash, in this part let's see how Flash is communicating with the server (mainly how the client is sent to how to send information to server-side [Remote Computer], and how to receive Information sent by the server-side [Remote Computer], as for the server-side Socket how to handle the part will speak in the back), the next part is the communication of the Flash and web pages, and it is best to read these two parts before the ActionScript in Flash. It is very similar to JavaScript.

1. Introduction to Xmlsocket Object In an overview, it has already mentioned the XMLSocket object in the flash, which is the core of communication between Falsh and Server Socket. It allows the browser that contains the flash application to establish a socket connection with the server, then the flash application can send XML data with the server, and after a socket connection is established, the amount of data transmitted on the connection is not limited, until Socket connection is closed. Xmlsocket objects A biggest benefit is to encapsulate your data in XML format, so you can easily process a variety of complex data in server-side or Flash. Xmlsocket objects only 3 methods and 4 events:

(1) method of .xmlsocket:

1. Connect (server address, port number): Try to join remote computer example: if (! MySocket.connect (null, 2000)) {mytextfield.text = "Connection failed!";} Where the connection has two parameters, The first parameter represents the host to connect, which can be a fully qualified domain name and person IP address, you need to pay attention to a point: When using an IP address, such as 127.0.0.1 needs to handle it as a string, that is, use quotation marks The IP address is enclosed. If you are null, the IP address where the web server (from the web server is downloaded from the web page containing the current Flash app). The second parameter represents the port to be connected, since the port below 1024 is occupied by the general program, the FLASH's security rule does not allow a connection to the port below 1024. The Connect method returns the Boolean variable True or FALSE indicating whether the connection is successful. In the above statement, if the connection fails, the Connect method returns FLASE, set the contents of MyTextField (instance of a non-static text textfield object to "connection failure!".

2. Send (information content): After establishing a connection with the remote computer, send information to the remote computer example: mysocket.send (""); where the parameters can be An XML string, or an XML object. If it is an XML object, the Send method will turn the object into a string, then send the string to the server, and add 0 words after the string is sent. Section. The Send method has no return value.

3.close (): Close up and remote computers: mysocket.close ();

(2) Events of .xmlsocket:

1. Onconnect (online results) When the connection is completed, it triggers and passes a parameter representing the successful parameter to the onConnect () event. If the online success, its value will be true. 2. Ondata () When the XMLSocket receives the information incoming remote computer, the OONDATA () event is triggered. It is different from the onXML () event in the following lies in the original string from the onData () event is the original string that has not been parsed by Flash, and from the onXML () event is the resolution XML data. Because the exchange between the different XMLSocket objects is an XML format, the onXML () event is commonly used.

3.onxml () When Xmlsocket receives XML data incorporated by the remote computer, the ONXML () event is triggered. In the onXML event, use the data obtained in the onData event to generate an XML object, and transmit the object as the parameter to the ONXML event, so if you want to customize the overxml event, the data sent by the server must be Is an XML format, otherwise it will happen unexpected errors. If the overdata event's processing function is set, the process function of the ONXML event will no longer call the overxml event, unless it is reversed, two events are mutually exclusive.

4. OnClose () This event will be triggered when the remote end computer is interrupted.

2. The process of using the XMLSocket object is:

(1) Create an XMLSocket object

MySocket = new xmlsocket ();

(2) Set the generated XMLsocket object

MySocket.onConnect = myonconnect; mysocket.onxml = myonxml; mysocket.onclose = myonclose

Function myonconnect (bool) {... // After the connection is completed, the parameter is the BOOL value, indicating whether the connection is successful} Function MyonXML (DOC) {... // Trigger when there is received data, data parameter DOC is taken From the XML object from the server, we can analyze the data we need in this function, and do the corresponding action} Function myonclose () {... // The connection is triggered, you can do some later work, such as Prompt users on the UI}

The first three statements set the three event handlers of MySocket, where MyonConnect, MyonXML are functions with a parameter, and MyonClose does not have parameters. When the corresponding event occurs, the corresponding processing function is called.

(3) Creating the connection with the server using the connect method of the XMLSocket object, establishing the connection mysocket.connect (NULL, 2000);

IF (! MySocket.connect (NULL, 2000)) {... // Connection failed}

In the above statement, if the connection fails, the Connect method returns FLASE. The XMLSocket object is connected to the remote computer to trigger an onConnect event, and the corresponding event handler (see the settings in step 2 above) MyonConnect, where the parameters are the same as the return value of the Connect method.

(4) After the connection is successful, the client can send XML data with each other. Send data to the server using the send method of the XMLSocket object:

Var myxml = new xml (); var myLogin = myXml.createElement ("login"); myLogin.attributes.username = "possible"; myLogin.attributes.password = "mvpcn"; myxml.appendchild (myLogin);

MySocket.send (MyXML);

You can also be just like this:

MySocket.send ("");

(5) Finally, when the program ends, use the Close method of the XMLSocket object, close the socket connection, as follows:

MySocket.close ();

It should be noted that using the Close method of the XMLSocket object to close the Socket connection does not trigger the onClose event of the XMLSocket object, only when the Socket connection is turned off by the server, the event is triggered at the flash application client.

3, you must pay attention to two points when using the XMLSocket object:

(1), in the Socket connection, the XML data sent, each data is separated by 0 bytes, because the XMLsocket's Send method is sent after the string is sent, and the additional 0 byte is sent. (2) The host connected to the flash application must be with the corresponding web server in the same IP address or the same subdomain. The same subdomain refers to the same domain space, such as a web page containing a flash application, is downloaded from blog.mvpcn.com, then possible.blog.mvpcn.net is the subdomain, allowing to establish a connection, and mvpcn.net is not Subdomains, Flash's safety rules are not allowed to establish a connection.

转载请注明原文地址:https://www.9cbs.com/read-113107.html

New Post(0)