ActionScript for Multiplayer Games and Virtual Worlds: Introducing ElectroServer
IN CHAPTER 2, Connecting Users, we introduced socket servers—software that is usually running in a remote location, accessible over the Internet, that manages thousands of connections between client applications (in our case, games and virtual worlds). ElectroServer is one of the most-used socket servers for multiplayer Flash content.
In this chapter, you will be introduced to concepts and terminology specific to ElectroServer, as well as installing it and writing a simple hello-world application. We’ll also look at how to configure the server using the web-based administration system.
ElectroServer is free and unrestricted for up to 25 connected users (at the same time). You can download and install it at www.electro-server.com/downloads.aspx.
In this section, we look at ElectroServer concepts and some terminology. Most of these concepts are popular and are shared by other servers as well. Since they are generally found in most socket server choices, the concepts described here are useful to learn about beyond ElectroServer itself.
A user is a representation of a client connected to the server (and logged in). It is possible for a single client to establish more than one connection to the server and still be seen as a single user. Because of that, it should be noted that while a user usually has only one connection to the server, he can have more than one. For instance, to stream video from the server to a client using ElectroServer, a user establishes a second connection to handle the audio/video stream.
A room is a common concept in the realm of socket servers that means a collection of users. In ElectroServer, a room is a way for one to many users to see each other and interact. If a user is in a room, then that user can send a chat message, which is then broadcast to all users in that room. That is a simple example of the use of a room. A user can be in multiple rooms at a time.
There are two types of rooms in ElectroServer:
Persistent—A room that always exists, even if there are no users in it. A persistent room allows users to join and leave at any time.
Dynamic—A room that is created for a single use. When the user list for that room drops to 0, meaning that all users have left, the room is destroyed. This is the most common type of room.
Rooms have many uses, the two most common of which are enabling chat and grouping users together to play a multiplayer game. Both of these common uses are explored in detail in later chapters.
A zone is collection of rooms. The concept of zones is mostly valuable as an organization tool for servers with large numbers of rooms. Rooms within a zone must be uniquely named. All users in a room within a zone have access to the room list for that zone. The user can subscribe to automatic list updates as the list of rooms changes.
If the room list is very large or very active (with many rooms being created and destroyed), you may find that connected users may receive too many updates per second to keep up with. Using more zones is one way to attempt to manage this.
Chat is the primary method by which users interact with each other while connected to a socket server. A chat message is text sent from one user to other users. In ElectroServer, as in many other socket server solutions, there are public and private chat messages. A public chat message is sent from a user in a room to that room at large—that is, broadcast to all users in that room. Typically, a public chat message would end up being displayed on the client in a text field, such as the one seen below.
A private chat message is sent from one user directly to one, or to many, specific other users. Unlike with public messages, the target user does not need to be in the same room as the sender, or in any room at all, for that matter. Most typically, a private chat message is from just one user to one other user, for example, to hold a private conversation between users in a chat room or to allow for private communication between players on the same team in a game. Many times in multiplayer games and virtual worlds, private messaging happens only between users who are considered buddies.
Social networking is an enormous part of the typical multiplayer experience. In addition to playing games and competing, users like to form relationships that transcend a single play session. They like to flag each other as buddies to add to a list. When a user returns to the application on a future session, she sees her buddy list and can tell which of her buddies are currently online and which are not. A user can then message any of her buddies that are currently online and even challenge them to a game.
Some virtual worlds or social networks allow users to send messages to their buddies even when they aren’t online. When the offline user returns, he sees the delayed message that was sent.
This is the first concept introduced that is truly ElectroServer-specific. An EsObject is something that may look a little odd at first, but is tremendously useful as you become familiar with it.
An EsObject is a class that exists in identical form on both the server and the client. A new instance of the class is created and data is then stored on this object. The object can then be exchanged between the client and the server easily, because the transaction layer knows how to serialize and deserialize the object.
EsObjects are used all through the API for client-server communication. Here is an example EsObject created using ActionScript:
var esob:EsObject = new EsObject(); esob.setString("name", "Jobe"); esob.setInteger("age", 33); esob.setStringArray("petNames", ["elfie", "bosley", "clyde"]);
You can see in the example above that first you create a new instance of the EsObject class, and then you add the data. Each property that you add to an EsObject has a strict data type. In the example above, you can see that String, Integer, and String Array are represented.
Here is the complete list of data types supported by EsObject:
- String / String Array
- Integer / Integer Array
- Number / Number Array
- Boolean / Boolean Array
- Byte / Byte Array
- Character / Character Array
- Double / Double Array
- EsObject / EsObject Array
- Float / Float Array
- Long / Long Array
- Short / Short Array
You may wonder at first why putting all data on an EsObject is a good idea. It seems like it takes a good bit of code, and having to identify the data types seems like overkill. Well, it comes down to saving time (and headaches) later. By using EsObjects and strict data types, you reduce ambiguity in your code and are forced to program in a way that ends up being more manageable down the road.
Another benefit—both more and less visible—of using EsObjects is bandwidth. The serialization process used for EsObjects (which is hidden from you) generates packet sizes that are miniscule and hence keeps bandwidth to a minimum.
In addition to providing a highly scalable connection layer, the most useful socket servers primarily provide a basic level of functionality right out of the box, like rooms, chatting, buddies, and a few other things. But what about features and functionality that you need for your game or world that are not part of the core feature set of the server you are using? A good socket server provides a way for you to add these features yourself. By extending the functionality of the server with your own custom code, you can achieve anything that you want to do.
ElectroServer and many other servers provide support for what are called extensions. An extension is custom code run on the server to provide features and functionality not built into the server.
An extension can include one or many types of objects to extend certain functionality of ElectroServer. There are three types of objects supported to extend and enhance ElectroServer: event handlers, plugins, and managed object factories. Each object can include variables to define values that should be known to the object upon creation.
Event handlers allow a developer to have some custom logic applied as a result of an event. These are typically written in Java, although they can be written in ActionScript 1 as well.
ElectroServer currently allows event handlers to be created for the following event types:
- Login—User is logging in to ElectroServer.
- Logout—User is logging out of ElectroServer.
- User Variable—User has updated the EsObject attached to the user object.
- Room Variable—User has updated the EsObject attached to the room.
- Buddy List—User has updated his buddy list (add / edit / delete).
- Private/Public Messaging—User has sent a message to another entity (room / user).
Let’s take the Login event handler as an example. A client connects to ElectroServer and provides his login credentials. His name and password can then be authenticated against a database. The event handler then decides if it should accept or reject the user’s connection.
Plugins (often written in Java) provide extended functionality where the core features do not offer what is needed. Clients can talk to plugins and plugins can talk to each other. ElectroServer supports two types of plugins: room and server.
Room plugins are in charge of any functions that may need to be attached to any particular room. Most often, these are used to handle game logic and additional room functions. A good example of a room plugin would be a card game: the plugin would handle all of the logic for dealing cards, which players receive them, score calculation, and deciding the winner. A room-level plugin is created and scoped to a room; thus, it is instanced. Clients can talk to any room-level plugin that is scoped to a room that they are currently in.
Server plugins extend the abilities that ElectroServer can perform. These may range anywhere from globally enhancing room functions to exposing an external interface to other applications that wish to leverage certain aspects of your game. Server-level plugins are created once and always exist, as opposed to room-level plugins, which are created as needed. Clients can talk to any server-level plugin.
A good example of a server-level plugin is accessing a remote RSS feed. If your virtual world required news entries to be displayed in-world in some way, then the server would need to load that news. In this case, it would be smart to have a server-level plugin that loads and manages the latest news, so that any other plugin can access it as needed.
Managed Object Factories
Managed object factories allow you to create instances of objects that need to remain active over time. One common object created through this kind of factory is a database connection. This will allow you to define the properties and database type to connect to and retrieve data from within a plugin.
Here’s an example of the creation of a MySQL-based managed object written for a Java-based plugin:
EsObject esDB = new EsObject(); esDB.setString("poolname","mysqlpool"); Connection c = (Connection)getApi().acquireManagedObject →("ManagedDBConnection", esDB);