Programming the Server


The last major topic we're going to talk about involves the way we can program the server to do specific tasks for us. This allows us to develop highscore boards and even multiplayer games . This section is going to show you how this is done so that you understand the overall system that is used to communicate between server and client as well as give you a taste of how to program the server.

To send information to the server, we'll use the functions you've seen before, primarily getURL and loadVariables . Recall that these functions had an extra argument that we didn't talk about. We'll use this to tell Flash to send some data from our Flash game to the server so that the server can do some processing or storing of that data.

After we have sent data to the server, we need to write some code for the server to process this data and do something meaningful with the result. After the processing is done, we can store information on the server as well as send information back to the client about the results of the processing.

The obstacle in developing this is that we cannot use Flash (ActionScript) on the server. Instead, we need another language that is capable of doing our server-side processing. We have many options for this, depending on the complexity of the processing, the storage requirements, the frequency of updates, and so on.

Tip  

Macromedia also has a server software called Flash Communications Server. This allows for extremely robust communications between a Flash movie and the server. You can establish socket connections (persistent connections) for real-time gaming, or even send microphone andWeb cam data through the server.

You could, for example, buy expensive Sun hardware and build a cluster of UNIX servers running Java programs that use Java servlets to capture the data coming from the client, Java's JDBC for storing this information in a database, and perhaps some Enterprise Java Beans in between to do the processing. However, for our purposes, this is overkill. If you were building a large e-commerce site, this might be your solution. But to implement a highscore board for a Flash game on the Web, we don't need something so complex.

Common Gateway Interface

Common Gateway Interface (CGI) is just a standard; it's not a programming language. This means that you have a great deal of flexibility in what language you want to use to program your server. You have just two requirements when choosing a language for CGI:

  • Choose a language that supports CGI. A partial list of languages that support CGI includes C, C++, Perl, Python, PHP, Java Server Pages (JSP), and Active Server Pages (ASP), to name a few of the more common ones.

  • Your Web hosting solution must support the language.

Note  

ASP is a Microsoft invention. It's common for Web hosts running Windows to support ASP.

JSP is Java's take on the same thing. Java is the primary language used on UNIX and Linux servers to handle Web services.

The Apache Software Foundation developed PHP. PHP is commonly found on UNIX and Linux systems, although most Windows servers support PHP as well. PHP is a recursive name that stands for PHP Hypertext Preprocessor.

For example, years ago I preferred to do CGI in C++ because I knew the language well and I could develop CGI programs quickly in it. C++ is not really the best language to do CGI in, and I knew it, but it was much faster than learning Perl or something similar. The problem is that when you program CGI in C++, you create a binary executable program (an .exe). This is fine if your Web host allows you to run an .exe on its server. Often, a Web host frowns on this because it cannot easily look into the code and see what it's doing. Most Web hosts feel much safer if you're using an interpreted language such as Perl or Python, where they can open your file and read what the code does ( assuming they can read Perl or Python source code). Over time, I had jobs in which I was not allowed to use executable CGI programs, and that forced me to learn another language: Perl.

Embedded Languages

I want to mention one more thing before we get into the actual server programming. You can use languages such as PHP, ASP, and JSP to write CGI programs. In my experience, however, these languages are best employed when you are writing programs to create HTML documents on-the-fly . In other words, if you need to produce an HTML document for the client based on each specific client, a language such as PHP, ASP, or JSP is a good choice.

PHP, ASP, and JSP are often called embedded languages because you typically place the code for them inside the HTML document (like JavaScript). When the client initially loads the page, the server takes the PHP, ASP, or JSP script and runs it, creating an HTML document for the client based on the script. The client's HTML does not have any of the scripting language in it when he receives it; the code is placed in the HTML document to make development easy. When it is time to serve the HTML to the client, the server runs the script that tells it how to construct the HTML document that the client has requested .

Other Language Choices

In contrast to embedded languages, Perl and Python have scripts that stand alone, usually in a directory called cgi-bin. When you have processing to do on the server that does not require you to create HTML documents, these languages are usually easier to work with.

Caution  

Some of the cheap Web hosts do not allow any kind of CGI to run. If this is the case, you need to consider getting either an upgraded Web hosting package with your current host, or a new Web host that allows your CGI scripts.

In our case, we don't need to make HTML documents, so I would prefer not to use an embedded language. This means we're going to be using either Perl or Python. Of the two, more Web hosts support Perl.

Because the average reader will more commonly support Perl than any other CGI-compatible language, I'm going to use Perl in this book. Perl also happens to be quite good at doing just the kind of work we need to implement a highscore board.

Perl

Obviously, we don't have time to get into Perl programming. In fact, I'm not going to be able to explain any of it. Some things about Perl make it quite different from ActionScript, and just getting your feet wet would require several chapters. In addition, not everyone will want to use Perl, so those chapters might be wasted . As a result, I'm going to provide you with some generic Perl scripts that will run a highscore board, assuming that your Web host allows Perl scripts. You should be able to make the minor modifications necessary to get a simple highscore board of your own going. From there, you can pick up a book on Perl if you would like to learn how to make a more robust and sophisticated system.

If you choose another language, you can just follow the logic in the Perl scripts and develop the same thing in another language. I will be explaining what each line of the Perl script does so that you can see the logic behind it. However, I won't explain why the line does what it does; that would require an explanation of Perl beyond what we have time for.

Before we get into the generic Perl scripts, I want to get the Flash part set up because it's familiar ground for us. After Flash is doing everything necessary on its end, we'll drop in the Perl scripts and I'll tell you line by line what the scripts are doing. From there, we'll have a working demo of a highscore board; the rest is up to you.




Macromedia Flash MX 2004 Game Programming
Macromedia Flash MX 2004 Game Programming (Premier Press Game Development)
ISBN: 1592000363
EAN: 2147483647
Year: 2004
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net