Implement the "push" of the web page with Flash and .NET Socket Communication

xiaoxiao2021-03-06  83

(1) Basic outline

Now the system of the B / S structure is increasingly popular, but because the B / S system is based on HTTP protocol, this determines its defects in real-time applications. Because the server-side information has occurred to the client (the "push" technique that has been hot and a moment is not allowed to get new data to request the client to get new data. Then there is a problem here, how can the client know that the server-side data has changed? When will I ask the server to request new data? Now in the B / S, the solution is to request new data to the server through the timing refresh of the client browser, but there is a certain amount of time error, in some real-time systems, such as the production line monitoring, etc. I still don't meet the requirements, and the client will still issue a request for obtaining data when there is no data change in the server side, which increases the burden on the server and network transmission. However, in the C / S system, we can easily send changes to the Client end by the Server terminal when the data changes can via the SCOKET channel. But how do we use Scoket in B / S? We may think of using ActiveX controls, but this involves issues such as digital signatures, and the production process is also troublesome. In fact, Flash has provided us with an XMLSocket object to implement the client socket, allowing the browser that contains the flash application to establish a socket connection with the server, and then the Flash application can send XML data with the server, and in a Socket connection After establishing, the amount of data transmitted on this connection is not limited until the socket connection is turned off. Only the page on the page, the JavaScript on the page can be easily implemented, mutual control and call, so we can use Flash as a bridge to connect the web page and server socket communication on the browser, thereby achieving The server actively "push" the client's effect.

Next, we will introduce as an example with the socket and flash mx under .NET, which may be used for the SQL Server 2000, the web server is IIS6.0, web technology uses ASP.NET technology, language is C #.

(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). Xmlsocket method: 1. Connect (server address, port number): Try to join remote computer example: if (! MySocket.connect (null, 2000)) {mytextfield.text = "Connection failed!"; The Connect method has two parameters. The first parameter represents the host to be connected, which can be a fully qualified domain name and a person IP address. You need to pay attention to a point: When using an IP address, such as 127.0.0.1 needs to make it as a string Treatment, you have to enclose the IP address with quotation marks. 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:

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

New Post(0)