Use Flash and socket mechanism to bring about a database connection
Related Tags:
I. Introduction
For a traditional clients - server Background developers, transferred to the Flash ActionScript development and connect to a database definitely need for a lot of twists and turns. . NET or Java world, through direct call from the application of the database, you need only open a connection to the database and the implementation of database operations can be and in the Flash circumstances things are not so simple. In this article, I will introduce you to solve this problem.
Even in a non-traditional development environments such as Flash, database connections are very necessary. For example, you may want to store high in a game, or any conversation between the need continues to store user information. Moreover, the flexible database connection, the demand is growing because there are now more and more people are building programme based on Flash rich Internet applications.
Summarized, there are three ways to make a Flash application connect to the database:
Use XMLConnector component;
Using a combination of XML and CGI;
XMLSocket use category.
Must clearly understand that there is no direct link to Flash to a database; must have a Flash application and database to play an intermediary. The method described above for each use of such a mechanism - in every way to achieve slightly different and contain different features Flash.
The first method in the Flash help in a good note, I am not elaborate.
The second method used in Flash end of the XML server in the category with a combination of CGI application, it is better suited to network applications does not exist in the circumstances. This method uses a POST HTTP request to connect to the application server. In this better explanation, please refer to Flash help information (ActionScript reference guide> work with external data> sent from remote sources and loading variables> Using XML).
The third method (This article will focus on), for some reason, the documentation could be a little reference. However, from a programmer's point of view, it is to provide the most control methods. Flash for a non-programmer, it is most familiar with the interactive database analogy. According to information Flash, "XMLSocket… maintain an open connection to the server, which allows the server can be in the absence of a request from the client, the case immediately sent the arrival of news."
In order to demonstrate the use of the XMLSocket, I constructed a simple application: a Flash front-end database (see Figure 1). This query tool for the use of the users are very familiar with, because it implements the Common Design: screen provides authentication information field, there is a field for the input to be transmitted to the database SQL commands. Although it is impossible to anyone who will use Flash to build a fully functional database query tool, but in this example can be used to achieve any specific needs to be customized.

Figure 1. Connect GUI: The image shows a Flash in building a database front-end.
Second, the Basic principles
The actual database operations through a back-end Java application (in the Flash Help known as the "daemon") to achieve, and it through JDBC (Java Database Connectivity) connection to the database. I chose Java because of its cross-platform and also because it ActionScript Syntax easy for developers to understand. Based on. NET looks back at the concept stage with similar.
Even if you do not know Java, if you have some experience using ActionScript, then you should understand this without any problem. If you are a Java or. NET programmers and without a solid foundation of experience in Flash, then I hope this article will provide some information to help you use familiar with the concept of building your database connectivity framework.
This paper tests the attention of procedures you need:
Flash MX 2004 or Flash 8 (Professional Edition)
Java IDE (I use the Eclipse 3.0)
Microsoft SQL Server 2000 driver for JDBC
SQL Server 2000
Flash front-end and back-end Java through Socket Communications. In Java, divided into socket server socket (listen and respond to the request of arrival), and socket (to the request sent to the server socket) two categories. The integrity of the Java Socket discussions have gone beyond the scope of this article, so if you were not as familiar with the case, please refer to other online resources for the purpose of this article, as are two of the socket between applications through the network the number of connections has been sufficient.
Please note that only support Flash "client socket" - it can connect to a server, but it does not play to other customers socket server, and it is XMLSocket category achievable.
XMLSocket there are some restrictions:
Only may be connected to greater than or equal to 1024 TCP port;
By default, the security constraints will prohibit connect to a different domain running on the server applications (rather than SWF). This can override the default behavior to solve, as detailed in Flash reference documentation.
To enable meaningful communications, the two applications must be able to understand the use of a mutual agreement (in this case, we based on XML): a 0 at the end of the Flash XML files are sent to the back-end application and implementation certain operations (in our case, the database access).
Our query tool in the circumstances, in order to implement a database is a two-step process: First, the use of the documents connected to the database. Then, if successful connection, sending and execution of the order (as SQL statements). As I constructed for applications with the characteristics of (a session may include many services), and I would like to between Java and Flash to build a more complicated communications protocol, so I opted for this two-step process. However, it is possible to certification and the implementation of a combination of steps - sent to the Java application from Flash news simply include authentication information and commands and then, Java will be returned to the back-end SQL command output (or, In connection or failure of the implementation of circumstances, a corresponding error message).
In order to achieve a SQL Server database certification requirements provide the following information:
The IP address of the database server;
Database login and password;
Database;
The Java application IP address or DNS (IP database and it shares the same name, can be different).
Once the Java application testing to a connection attempt, it will receive it as much as possible of the XML messages. If it is an 'open connection' information, the application will be extracted from the database server IP address, login, password and database; it will try to open a connection to the database JDBC If successful, it will send a message and XML The only connection handle Flash back to front. It also will connect digital information and certification (a "handle") added to an internal connections on the set.
Follow a successful connection, the front sent a SQL commands, it also contains information on the connection handle. Once received a SQL commands, will extract part of the back-end connection handle authentication information and use it to the order sent to the database.
Sent by the front end connected to the opening of an XML format with the following information:
<flashLogon>
<IPAddr> 127.0.0.1 </ IPAddr>
<Login> Sa </ Login>
<Password> Admin_password </ Password>
<Database> User_database </ Database>
</ FlashLogon>
Please note that here there are two front IP address domain: an IP address or a Java application is running on his (port 1024), the DNS server name, and the other holders of the IP address of the database server; above <IPAddr> cited the database server.
Dealing with a SQL commands the news the following format:
<flashCommand>
<Command> SELECT * FROM Table1 </ Command>
<Connection> 1 </ Connection>
</ FlashCommand>
If the connection is successful, Java server applications will be returned to the following:
<Connection>
<HConnection> 1 </ HConnection>
</ Return>
Dealing with a command, and then returned to a similar to the following:
<Return>
120
</ Return>
However, this news may also be more complex, such as:
<Return>
Michaels, Rich, Acacia St 1200,10962
King, Mary, 98012,11128 Maryland Ave
</ Return>
It may also contain an error message, such as:
<Return>
SQL Error 101 Table does not exist.
</ Return>
Third, build front-end
Now, in this paper we analyze the SocketGUI.fla source document. Frame0 in, I loaded the background code files (GlobalFrame0.as). Among them, Frame1 contains GUI elements. In Frame1 of all button event processor in GlobalFrame0.as simple reference to the method.
Connect function against the 'Connect' button click functions. I use XMLSocket the connect function and the use of port 1024 (which may be greater than or equal to 1024 of any median 16) to try to open up a Java application to the connection.
XMLSocket open a connection using the two-step: First, the method of call connect and then call onConnect Flash incident processor to notify the application attempts to connect the success or failure in this processor, you must join code to deal with trying to open the link results.
To connect fully successful, in our sample application, we must ensure that the XMLSocket.connect Java application called the successful implementation and certification to the successful implementation of the database. If these two conditions are met, then the Java application will be returned to an XML encoding digital connections, and it stored in HConnection variable. When Flash received the news from the server, the onData XMLSocket events will trigger. Of course, the different response may be received: a successful connections will receive a digital connection, and a database will receive an order from the news database. ParseReturn function of the response received by the decision and the basis of this type of response continuation.
If it is successful, users will be able to send a command to the database; SendCommand this function in the function to achieve, and it is as simple as calling XMLSocket.send.
Connect and SendCommand will need appropriate formatting information to understand their Java application (the XML format described above). XMLCreateLogon and XMLCreateCommand is completed this task. As you will note, each source needs to be termination of a newline characters ( '\ n'), Java server socket would not otherwise receive this news.
By the Flash encoding the only remaining thing is to disconnect function in order to achieve disconnect, you can press' Disconnect 'button on the command line or send a' disconnect 'news. All of the features (basically deleted from the database set authentication and handle) in the server application to achieve in Flash, I just reset the connection handle.
Fourth, backend Construction
Java back-end application has two parts: a 'listener' category (referred to as oSckSrv), which uses Java socket to receive orders from the Flash XML and an 'executor' category (referred to as dbConn), it implements the practical Database communication.
The Java application is the core of an infinite loop (oSckSrv.java reference in the main document in the cycle). It simply port in 1024, waiting to receive a link request, upon receipt of the request connect immediately using a flow reader to read the XML messages. Trial in a variety of readers, I discovered BufferedReader Java in the most appropriate category receiving text (XML) string. Then, parseReceivedXML function to be called to explain the news. This function uses Document Object Model API analysis received from the Flash XML documents.
If you receive a 'logon' message, dbConn category getConnection function to be called to try to open the connection to the database. If you receive a 'command' message, the code will analyse whether the XML messages embedded in an effective (existing) connection handle. If the handle effectively, dbConn for transmission received from the Flash in the SQL information.
DbConn responsible for the realization of the actual database work. If you are familiar with JDBC, or other experience in the use of the agreement, such as ODBC or ADO, it is part of the code is very easy to understand. Connect function parameters using certified open a connection to the database, but the really interesting is the Execute function, since it uses Flash receive SQL commands the front of the string and pass it to the database server engine in any way to deal with it. Clearly, the Flash and Java do not know the information sent to the database; only by the database server to understand it!
Execute function of the database server from the XML format output return to the caller (oSckSrv), it then sends out to the front. The news must be a '\ 0' termination to the Flash XMLSocket correct receive it.
In my dbConn programming, I use the Microsoft SQL Server database. However, you can easily see that the Java applications do not have to connect directly to a database, a database server or a database platform. If you want to connect to the outside in addition to the SQL Server database server, you need to revise the code to the appropriate loading JDBC driver and front-end through the provision of appropriate connection parameters. You may also need to add a Flash front-end 'database type' server code parameters to help decide which run time loading drivers.
I must admit to some simple, I did point "little tricks." You will note that in ActionScript code, in the send method called XMLSocket before, I would again open the link. I could have been connected to the more open and ready to receive the request (via buffer readers ino); However, ServerSocket accept such methods being blocked, which means allowing further processing before it will wait for another connection request; At the same time, if I wait to receive from ino.readLine has been connected to a more customer orders, the Java application will not be in service from other customers requests for connections. I now use the programming approach is that once received a link request, the request shall services and closed by the server TCP / IP connection (this is the event that the Flash application, or connect to an order submitted to the Java application on the show 'Connection closed' news reasons), then, the application to wait for the return of another link request.
Java applications in the practical development of the socket read and write is generally in a separate thread implementation; Upon receipt of the request and a connection to a customer's connection has been opened, connecting the treatment should be independent in the main thread (now it is awaiting the opening of another link) implementation. This may increase the complexity of the Java coding; Therefore, in this paper, I ActionScript used "deceit" approach and open / close the connection, and to avoid dealing with multi-threaded Java problem. However, it must be noted that in practical applications, in order to reduce the open TCP / IP network connectivity related performance problems, it is possible need for a full-duplex connection session.
However, the greatest challenge is to deal with all kinds of unrestricted output (which may come from the front-end to the query results). A genuine query tool will be the use of a very complex agreement for the output format (for example, the use of a SQL Server Tabular Data Stream called the internal agreement, the client "description" of the response sent by the server), which has been As goes beyond the scope of this paper. To this end, I only realize a simple return to news - the first column of each record to <Return> labels closure tail. This response could be a database record set a return code or a news should be as specific SQL commands sent them. A practical application will need to use a complex XML tags embedded in the front-end response and achieve the same complex logic to decode this information.
However, in the practice of a Flash application, might not wish to deal with a wide range of database server response. For example, you may only need to be a user points and the highest number of users to choose. In such cases the establishment of a meaningful agreement will be quite simple.
5. Restrictions
Examples of this model to achieve the greatest challenges is the need to understand customer applications from server applications from the database to return to the metadata (structure) information. However, typical cases will include a Flash application - it only submitted to the database a limited number of enquiries and has been a limited number of responses, and this may make ParseReturn function of the size and complexity of the moderate. In fact, in the beginning of this article mentioned some other database connection method does require programmers to be the definition of XML information exchange model, it can not be evaded described to be receiving data.
Capacity is another limiting factor: a strong need to answer many customers call server application may need to use multiple threads, connection pool and other advanced technologies.
In addition, the Flash application and Java applications of communication between security very important: I examples of procedures might be sent "peeping" unencrypted data. A practical application in the data sent by the socket before the need to be encrypted.
Sixth, the establishment of environmental
You can take the Java application and database server installed on the same or different machines. In order to activate the Java application, you must be implemented under the DOS command (see attached source code in the launch.bat):
Set CLASSPATH =.;% CLASSPATH% C: / Program Files / Microsoft SQL Server 2000 Driver for
JDBC / lib / mssqlserver.jar; C: / Program Files / Microsoft SQL Server 2000 Driver for JDBC
/ Lib / msutil.jar; C: / Program Files / Microsoft SQL Server 2000 Driver for JDBC / lib / msbase.jar;
Java oSckSvr
Pay attention to the order is part of the focus of MS SQL Server JDBC driver to the path.
For little experience of Java developers, the establishment of small development environment may be a bit difficult. I use the Display in Figure 2 of the directory structure. I build up my environment so as to the Eclipse Java binary files copied to the \ bin subdirectory, as shown in Figure 3. Figures 4 and 5 show the application to establish a connection to the database and receive a response. 
Figure 2. Java application directory structure: This screen shot shows that the author's source directory structure. 
Figure 3. Java application directory structure: This screen shot shows the author's Eclipse in the binary files and boot files (launcher), the directory structure.
Author Note: you can download from Microsoft's site corresponding to the SQL Server JDBC driver. 
Figure 4. Connect to the database: This snapshot shows that the process of establishing a connection to SQL Server. 
Figure 5. Query and response: SQL Server received enquiries from Figure 4 and sent its response.
Flash allows customers to download from the Java application response (If these two procedures in the same domain is not the case), the web server on the need for a cross-border strategies of XML documents. For more details, please refer to Flash help.
In short, even though a database of typical communications in Flash is not as in other development environment, as a direct, but it also can help an "intermediary" (for example, the use of JDBC to access agreements or ADO database applications) to achieve .
- Web2.0 on B2B website operators bring ideas on what changes
- FLASH FLASH Avatar part Source Code Analysis
- Obedient to Flash: Flash interaction with the microphone
- Flash in the A * Pathfinding
- Fireworks flash simulation results
- The package JS / import mechanism outline of the study
- Flash converters Wang: Let ubiquitous Flash animation
- On the play, Flash and stop Statements
- Flash Media initial start Flash Media Server
- Flash brief description of the menu
- Xhtml ? ?FLASH
- Ajax error handling mechanism Technology
- Flash pages through calibration
- Learning Flash: FLASH music on the part of settings
- Flash AS: Double-click support for the flash category
- Flash Flash China and the development of Thinking
- Dreamweaver Insert Flash parameters
- [] CSS techniques bring new standards of the new issues
- Rookie Flash: Flash material I will be "stolen" from
- Flash AS3.0 powerful mechanism incident
- Web2.0 bring new applications and new experience
- Flash components [Flash Video Guide]
- Pixel in the flash of
- Use Flash and socket mechanism to bring about a database connection
- Site model analysis: Network pick in the end we have to bring any more
- Ajax error handling mechanism
- Flash AS Learning: to flash plus a trim () function
- Flash
- Flash Forecast
- Use Flash and socket mechanism to bring about a database connection




