You are previewing HTML5 Canvas.

HTML5 Canvas

Cover of HTML5 Canvas by Steve Fulton... Published by O'Reilly Media, Inc.
  1. HTML5 Canvas
    1. SPECIAL OFFER: Upgrade this ebook with O’Reilly
    2. A Note Regarding Supplemental Files
    3. Preface
      1. Running the Examples in the Book
      2. What You Need to Know
      3. How This Book Is Organized
      4. Conventions Used in This Book
      5. Using Code Examples
      6. We’d Like to Hear from You
      7. Safari® Books Online
      8. Acknowledgments
    4. 1. Introduction to HTML5 Canvas
      1. The Basic HTML Page
      2. Basic HTML We Will Use in This Book
      3. The Document Object Model (DOM) and Canvas
      4. JavaScript and Canvas
      5. HTML5 Canvas “Hello World!”
      6. Debugging with Console.log
      7. The 2D Context and the Current State
      8. The HTML5 Canvas Object
      9. Another Example: Guess The Letter
      10. What’s Next
    5. 2. Drawing on the Canvas
      1. The Basic File Setup for This Chapter
      2. The Basic Rectangle Shape
      3. The Canvas State
      4. Using Paths to Create Lines
      5. Advanced Path Methods
      6. Compositing on the Canvas
      7. Simple Canvas Transformations
      8. Filling Objects with Colors and Gradients
      9. Filling Shapes with Patterns
      10. Creating Shadows on Canvas Shapes
      11. What’s Next
    6. 3. The HTML5 Canvas Text API
      1. Displaying Basic Text
      2. Setting the Text Font
      3. Text and the Canvas Context
      4. Text with Gradients and Patterns
      5. Width, Height, Scale, and toDataURL() Revisited
      6. Final Version of Text Arranger
      7. What’s Next
    7. 4. Images on the Canvas
      1. The Basic File Setup for This Chapter
      2. Image Basics
      3. Simple Cell-Based Sprite Animation
      4. Advanced Cell-Based Animation
      5. Applying Rotation Transformations to an Image
      6. Creating a Grid of Tiles
      7. Zooming and Panning an Image
      8. Pixel Manipulation
      9. Copying from One Canvas to Another
      10. What’s Next
    8. 5. Math, Physics, and Animation
      1. Moving in a Straight Line
      2. Bouncing Off Walls
      3. Curve and Circular Movement
      4. Simple Gravity, Elasticity, and Friction
      5. Easing
      6. What’s Next?
    9. 6. Mixing HTML5 Video and Canvas
      1. HTML5 Video Support
      2. Converting Video Formats
      3. Basic HTML5 Video Implementation
      4. Preloading Video in JavaScript
      5. Video and the Canvas
      6. Video on the Canvas Examples
      7. Animation Revisited: Moving Videos
      8. What’s Next?
    10. 7. Working with Audio
      1. The Basic <audio> Tag
      2. Audio Formats
      3. Audio Tag Properties, Functions, and Events
      4. Playing a Sound with No Audio Tag
      5. Creating a Canvas Audio Player
      6. Case Study in Audio: Space Raiders Game
      7. What’s Next
    11. 8. Canvas Game Essentials
      1. Why Games in HTML5?
      2. Our Basic Game HTML5 File
      3. Our Game’s Design
      4. Game Graphics: Drawing with Paths
      5. Animating on the Canvas
      6. Applying Transformations to Game Graphics
      7. Game Graphic Transformations
      8. Game Object Physics and Animation
      9. A Basic Game Framework
      10. Putting It All Together
      11. The player Object
      12. Geo Blaster Game Algorithms
      13. The Geo Blaster Basic Full Source
      14. Rock Object Prototype
      15. What’s Next
    12. 9. Combining Bitmaps and Sound
      1. Geo Blaster Extended
      2. Creating a Dynamic Tile Sheet at Runtime
      3. A Simple Tile-Based Game
      4. What’s Next
    13. 10. Mobilizing Games with PhoneGap
      1. Going Mobile!
      2. Creating the iOS Application with PhoneGap
      3. Beyond the Canvas
      4. What’s Next
    14. 11. Further Explorations
      1. 3D with WebGL
      2. Multiplayer Applications with ElectroServer 5
      3. Conclusion
    15. Index
    16. About the Authors
    17. Colophon
    18. SPECIAL OFFER: Upgrade this ebook with O’Reilly
O'Reilly logo

Multiplayer Applications with ElectroServer 5

The extended HTML5 specification includes an API for what is known as WebSockets. Socket communications allow a client application to remain constantly connected to a server-aide application. This type of communication can be especially useful for chat, multiplayer games, e-learning applications, and multiuser whiteboards, as well as many others. At this point, WebSockets is just a promise with very little support beyond test and development builds of web browsers. In fact, security concerns have halted some implementations and slowed others. However, this does not mean you cannot make use of socket-server applications with HTML5 Canvas.

Because Flash has built-in support for communication via sockets, its applications have had the ability to open socket communications with server-side applications for many years. HTML, on the other hand, has never had the ability to reliably communicate to a socket server without performing some sleight of hand, usually involving constant polling by the web browser for new information from the web server.

ElectroServer from Electrotank was one of the first reliable socket-server applications built to communicate with Flash clients. Over the past couple years, ElectroServer has been updated with APIs for iOS, C#, C++, and now JavaScript. This first iteration of the ElectroServer JavaScript API does not use WebSockets, but instead implements JavaScript polling. However, with the availability of ElectroServer’s simplified JavaScript API, you can still start to write multiplayer applications using HTML5 Canvas.

Note

While this portion of the chapter is specific to ElectroServer, many of the multiplayer/multiuser concepts are applicable to other technologies as well.

Installing ElectroServer

To get started with multiplayer development using HTML5 Canvas and the ElectroServer socket server, you first need to download the free, 25-user version of the software from Electrotank. You can download the appropriate version for your operating system (Windows, Mac, Linux) at http://www.electrotank.com/resources/downloads.html.

Note

There are some installation prerequisites, such as having Java version 1.6. For detailed installation instructions for every OS, visit http://www.electrotank.com/docs/es5/manual/index.html?operating_system.htm.

The install package includes the server software, client APIs, documentation, and sample applications. Once you have installed the server software, you should have a folder named something like “Electroserver_5_x_” on your computer. We used Mac OS X for this test, so this folder was created inside the Mac Applications folder. On Windows, it will be created in the location you specify upon installation.

Starting the server

Once you have the files installed, you need to start the ElectroServer socket server by finding the installation directory and executing the file Start_ElectroServer_5_0_1. (Note: the three numbers at the end of this file will change as the version is upgraded, but the concept will remain the same.)

When ElectroServer starts, you should see a screen similar to Figure 11-3.

ElectroServer started

Figure 11-3. ElectroServer started

The server will run on your local machine for testing purposes. However, for any real-world application, you will need to install a production version of the software on a web server.

The ElectroServer admin tool

Because ElectroServer is a socket server, it listens on a specified port for communication from the JavaScript client using one of the supported protocols. ElectroServer supports multiple protocols, but we need to make sure we are using the BinaryHTTP protocol for the JavaScript API. The default port for BinaryHTTP in ElectroServer is 8989.

Note

When the ElectroServer JavaScript API is updated to support WebSockets, the port and protocol will likely be different.

There is a nifty admin tool for ElectroServer that allows you to view and modify all the supported protocols and ports, as well as many other cool features of the socket server. In the /admin directory of the install folder, you should find both an installer for an Adobe AIR admin tool (named something like es5-airadmin-5.0.0.air), and a /webadmin directory with an HTML file named webadmin.html. Either one will work for this exercise.

Note

In order for the admin console to display properly, the server needs to be started.

When you launch the admin tool, you will be asked to supply a username and password. The default is administrator and password, unless you changed them upon installation.

Once you log in, click the Server Management button on the top menu, and then the Gateways option from the side menu. You should see a screen that looks similar to Figure 11-4.

ElectroServer ports and protocols

Figure 11-4. ElectroServer ports and protocols

This screen shows you the port settings for each protocol that ElectroServer supports. For the JavaScript API, we are most interested in the BinaryHTTP setting, which you can see is set to port 8989.

The JavaScript API

Besides starting ElectroServer, you will also need the JavaScript API so you can begin building Canvas apps that connect to the server. You should be able to find the JavaScript API in the /apis/client/javascript directory of the folder in which you installed ElectroServer (this name might change in the final version). The API should be named ElectroServer-5-Client-JavaScript.js.

The Basic Architecture of a Socket-Server Application

Now that you have ElectroServer ready to go, and you have the JavaScript API, it is time to learn a bit about how socket-server-based multiplayer/multiuser applications are designed. Using a socket server means you are creating an application that relies on a client for input from a user, as well as relying on a server to distribute that input to other users who are connected to the first user.

A good example of this is a chat application. Most chat applications require a user to enter a room (a logical space in which people are “chatting,” i.e., exchanging messages), where that user can see the messages of other people in the same virtual space. In that room, the client is “connected” to those other users. However, it is usually not a direct connection (e.g., peer-to-peer), but instead a connection through a port to a socket server.

The socket server acts as the traffic cop for the chat messages. It listens on a port (in our case, 8989) for messages coming in from the clients. Those messages need to be formatted in a way that the server can understand so it can process them. The JavaScript API we will use performs this formatting for our client applications.

When the socket server receives a message from the client, it routes the various text messages sent by each client back out to the other clients in the room. However, it can also do much more by using server-side processing, such as hold the list of current messages, so people entering the room while the chat is ongoing can see what has been said previously, scan chat messages for swear words, award points to users for their input, or anything else you can dream up.

When the server finally processes the message and sends it back, the client then processes that message. In the case of the chat, that processing usually involves displaying the message on the canvas.

The Basic Architecture of an ElectroServer Application

ElectroServer acts very much like the socket-server application we described in the previous section. It listens on specified ports for different protocols; when messages arrive, they are routed back to the connected clients.

However, ElectroServer has some specific features that we should discuss. Some of these exist on other socket-server platforms, while some don’t. However, much of this discussion will still be applicable to other socket servers once they make JavaScript APIs available.

Client

The client for an ElectroServer application is a program written in one of the API-supported language platforms, including Flash ActionScript 2, Flash ActionScript 3, Java, Objective-C, C#/.NET, and now JavaScript. The client is the application, which the user will manipulate to send messages through the API to ElectroServer. This is usually a game, a chat room, a virtual world, or some other kind of multiuser social or communication application.

All the communication with ElectroServer is event-based. The client application uses the JavaScript API to send events, and the client defines event handlers that listen for messages from ElectroServer. All of these messages and events are communicated through the API, which in turn is communicating through port 8989 using the BinaryHTTP protocol (at least for our examples).

Zones, rooms, and games

When a user first connects to ElectroServer, she needs to join or create a zone, which is simply a collection of rooms. If the user tries to create a zone that already exists, she will be added to that zone without creating a new one.

After entering a zone, the user needs to join a room in that zone. If a user attempts to create a new room that already exists, she will be added to that room instead.

Note

Beyond zones and rooms, ElectroServer also offers a GameManager API that allows you to further segment users into specific instances of a game that is being played. We do not get this granular for the examples in this chapter.

Extensions

Extensions are server-side code modules that can process data sent by clients before that data is sent back to other clients. Extensions can also process and create their own events. For many games, the extension contains much of the game logic, relying on the clients for displaying and gathering user input.

At the very minimum, an extension contains what is known as a plug-in. A plug-in is a code module written in ActionScript 1 (basically JavaScript) or Java that can be instantiated and scoped to a room. For example, if you were making a card game, you would want a card game plug-in on the server to handle things like shuffling the deck and making sure the correct player wins a hand. In this way, the server holds the true state of the game. Using an extension helps keep a game flowing and lessens the users’ ability to cheat. For the simple examples in this chapter, we will not be using any server-side extensions. However, if you delve further into ElectroServer or other socket-server applications, you should make sure to learn as much as possible about them.

Creating a Chat Application with ElectroServer

As an example, we are going to create a single chat application using the ElectroServer JavaScript API. Users will submit a chat message through an HTML form, and the displayed chat will be in HTML5 Canvas. We are also going to create and display some messages from ElectroServer so you can see the status of the connection to the server.

Establishing a connection to ElectroServer

First, a client application is written so that it includes the ElectroServer JavaScript API:

<script src="ElectroServer-5-Client-JavaScript.js"></script>

The client application makes a connection to ElectroServer running on a server at a specific URL, listening on a specific port, using a specific protocol. For our examples, this will be localhost, 8989, and BinaryHTTP, respectively.

We need to use these values to make a connection from the client to the server. We do this by first creating an instance of the ElectroServer object, and then calling its methods. We start by creating an instance of an ElectroServer server connection named server. We then configure a new variable named availableConnection with the previous properties we described, then add it to the server variable with a call to the method addAvailableConnection(). We will create all of this code inside our canvasApp() function:

var server = new ElectroServer.Server("server1");
var availableConnection = new ElectroServer.AvailableConnection
    ("localhost", 8989, ElectroServer.TransportType.BinaryHTTP);
server.addAvailableConnection(availableConnection);

Now, we need to use the server variable we just configured to establish a connection to ElectroServer. We do this by setting a new variable, es, as an instance of the class ElectroServer. We then call its initialize() method and add the server we just configured to the es object by calling the addServer() method of the ElectroServer server engine property:

var es = new ElectroServer();
es.initialize();
es.engine.addServer(server);

We are almost ready to try to connect to ElectroServer. First though, we need to create some event handlers for ElectroServer events. Remember when we told you that all the communication with ElectroServer is done through creating and listening for events? This is where that process begins. We need to listen for the following events: ConnectionResponse, LoginResponse, JoinRoomEvent, JoinZoneEvent, ConnectionAttemptResponse, and PublicMessageEvent:

es.engine.addEventListener(MessageType.ConnectionResponse, onConnectionResponse);
es.engine.addEventListener(MessageType.LoginResponse, onLoginResponse);
es.engine.addEventListener(MessageType.JoinRoomEvent, onJoinRoomEvent);
es.engine.addEventListener(MessageType.JoinZoneEvent, onJoinZoneEvent);
es.engine.addEventListener(MessageType.ConnectionAttemptResponse, 
    onConnectionAttemptResponse);
es.engine.addEventListener(MessageType.PublicMessageEvent, onPublicMessageEvent);

Finally, once we have everything ready, we call the connect method of the ElectroServer object, and wait for events to be handled by the event listener functions we have just established:

es.engine.connect();

When the ElectroServer API object tries to connect to an ElectroServer server, a ConnectionAttemptResponse event will be fired back to the client from the server. We handle that event with the onConnectionAttemptResponse() event handler. For our application, we don’t do anything with this event, except create a status message for it that we will display. The statusMessages variable is an array of messages that we keep around to display back as debug information for our chat application. We will discuss this briefly in the next section:

function onConnectionAttemptResponse(event) {
 statusMessages.push("connection attempt response!!");
}

At this point, the client waits for a ConnectionResponse event to be sent back from the ElectroServer server. When the client application receives a ConnectionResponse event, it handles it with the onConnectionResponse() event handler. Once the connection is established, the client then attempts to log on to the server. To make a logon attempt, we need a username. We will create a random username, but it could come from an account on a web server, a form field or cookie, Facebook Connect, or any other location or service you might have available.

After we have a username, we create a LoginRequest() object, set the userName property, and then call the send() method of the es.engine object. This is how we will send all messages to ElectroServer from this point forward:

function onConnectionResponse(event) {
   statusMessages.push("Connect Successful?: "+event.successful);
   var r = new LoginRequest();
   r.userName = "CanvasUser_" + Math.floor(Math.random() * 1000);
   es.engine.send(r);
 }

When ElectroServer responds from the LoginRequest, it is time to join a zone and a room. Recall that any user connected to ElectroServer needs to belong to a room, and every room belongs to a zone. Therefore, we need to make a user belong to one of each, which we accomplish with a CreateRoomRequest(). We set the zoneName property to TestZoneChat, and the roomName property to TestRoomChat. If either of these do not already exist, they will be created by the server. If they do exist, the user will be added to them. We then send the message to ElectroServer:

function onLoginResponse(event) {
   statusMessages.push("Login Successful?: "+event.successful);

   username = event.userName;

   var crr = new CreateRoomRequest();
   crr.zoneName = "TestZoneChat";
   crr.roomName = "TestRoomChat";

   es.engine.send(crr);
}

We still need to wait for a couple responses from ElectroServer events that come back through the API via port 8989. We know we have to join a zone, and we handle the event with the function onJoinZoneEvent(), but we don’t need to do anything with it:

function onJoinZoneEvent(event) {
   statusMessages.push("joined a zone");
}

The most important event we are waiting to handle is JoinRoomEvent. When we receive this event, we know that we have joined both a zone and a room, and the application is ready to run. For the chat application, this means the user can start typing and sending messages. First, we set the _room variable equal to the Room object, which was returned by the event from ElectroServer. We will use this variable for our further communications with ElectroServer. The other thing we do in this function is set an HTML <div> with the id of inputForm, which is made visible by changing its style. The inputForm <div> is invisible when the page loads. We do this so the user won’t send chat messages before the connection to ElectroServer is established. Now that everything is ready to go, we display the inputForm <div> so chatting can start:

function onJoinRoomEvent(event) {
            statusMessages.push("joined a room");
            _room = es.managerHelper.zoneManager.zoneById
                (event.zoneId).roomById(event.roomId);
            var formElement = document.getElementById("inputForm");
            formElement.setAttribute("style", "display:true");
         }

Creating the chat functionality

Now that we have established a connection to ElectroServer and joined a zone and a room, the chat application can start.

First, let’s talk a bit about a few more variables we have created in our canvasApp() function, which we must scope to the rest of the chat application. The statusMessages array will hold a set of messages that we want to keep about the connection to ElectroServer. We will display these in a box on the right side of the canvas. The chatMessages array holds all the messages users have sent into the chat room. The username variable holds the name of the user who is running the Canvas application, and _room is a reference to the room object that user has joined:

var statusMessages = new Array();
var chatMessages = new Array();
var username;
var _room;

The HTML page holds a <form> that we will use to collect the chat messages from the user. It contains a text box for the user to type into (the id of textBox), and a button with the id of sendChat. This is the same form that was invisible until we received the JoinRoomEvent event:

<form>
<input id="textBox" placeholder="your text" />
<input type="button" id ="sendChat" value="Send"/>
</form>

In canvasApp(), we set up an event listener for when the user clicks the sendChat button. When a click event occurs, the function sendMessage handles the event:

var formElement = document.getElementById("sendChat");
formElement.addEventListener('click', sendMessage, false);

The sendMessage() function is one of the most important functions in this application. This is where we create a couple very critical objects for communicating with ElectroServer. The first is a PublicMessageRequest, which is one of several types we can make to the ElectroServer socket server. Others include a PrivateMessageRequest and a PluginMessageRequest. A PublicMessageRequest is a message that will be sent to everyone in the room. We send that data using an EsObject, which is native to the ElectroServer API. It allows you to create and access ad hoc data elements for any type of information you want to send to other users in the same room.

Note

For a full discussion of EsObject and ElectroServer events, see the ElectroServer documentation. It is installed with the server on your local machine in [your install folder]//documentation/html/index.html *.

For this simple chat example, we want to send the chat message the user typed and submitted. To do this, we will use the setString() method of EsObject. This method takes two parameters: the text you want to send, and an identifier you can use to access the text. We also set another element named type, which will tell us what kind of message we are sending. We do this because in a more complicated application, you may send all sorts of messages and need a way to identify what they are so you can process them.

Once we have configured our PublicMessageEvent with the roomId, the zoneId, and the EsObject, we call es.engine.send(pmr) to send it to the rest of the room:

function sendMessage(event) {
   var formElement = document.getElementById("textBox");
   var pmr = new PublicMessageRequest();
   pmr.message = "";
   pmr.roomId = _room.id;
   pmr.zoneId = _room.zoneId;
   var esob = new ElectroServer.EsObject();
   esob.setString("message", formElement.value);
   esob.setString("type","chatmessage");
   pmr.esObject = esob;
   es.engine.send(pmr);
   statusMessages.push("message sent")
}

Notice that we did not print the user’s chat message to the canvas when it was submitted. Instead, we will wait for the PublicMessageEvent to return from ElectroServer, and then handle it like all the other chats. This keeps the interface clean, while preserving a create event/handle event processing model across the entire application.

After the socket server processes the chat message, it is broadcast out to all the users in the room. All the users must create an event handler for a PublicMessageEvent so they can receive and process the message; we have created the onPublicMessageEvent handler for this purpose. This function is very simple. It checks the type EsObject variable we set to see whether it is a chatmessage. If so, it pushes a string that includes the user who submitted the message (event.userName) and the message itself (esob.getString("message")) into the chatMessages array. This is what will be displayed on the canvas:

function onPublicMessageEvent(event) {

   var esob = event.esObject;
   statusMessages.push("message received")
   if (esob.getString("type") == "chatmessage") {

      chatMessages.push(event.userName + ":" + esob.getString("message"));

      }

}

Now, all that remains is to display the messages that we have collected. We do this (where else?) in drawScreen(). For both the statusMessages and chatMessages arrays, we need to display the “current” 22 messages (if we have 22), and start them at the y position of 15 pixels. We only display the last 22 messages so both the chat and the status messages will appear to scroll up the screen as more chatting and status messages are generated:

var starty = 15;
var maxMessages = 22;

If the array is larger than maxMessages, we display only the latest 22. To find those messages, we set a new variable named starti to the length of the statusMessages array, subtracted by the value in maxMessages. This gives us the index into the array of the first message we want to display. We do the exact same thing for the chatMessages array:

//status box
   context.strokeStyle = '#000000';
     context.strokeRect(345,  10, 145, 285);
         var starti = 0;

         if (statusMessages.length > maxMessages) {
             starti = (statusMessages.length) - maxMessages;

         }
         for (var i = starti;i< statusMessages.length;i++) {
            context.fillText  (statusMessages[i], 350, starty );
            starty+=12;
//chat box
         context.strokeStyle = '#000000';
         context.strokeRect(10,  10, 335, 285);

         starti = 0;
         lastMessage = chatMessages.length-1;
         if (chatMessages.length > maxMessages) {
             starti = (chatMessages.length) - maxMessages;
         }
         starty = 15;
         for (var i = starti;i< chatMessages.length;i++) {
            context.fillText  (chatMessages[i], 10, starty );
            starty+=12;
         }
      }

That’s it! We’ve finished developing our multiuser chat application.

Testing the Application in Google Chrome

To test the current ElectroServer JavaScript API, you need to start Google Chrome with web security disabled. The method of doing this varies by OS, but on Mac OS X, you can open a Terminal session and execute the following command (which will open Chrome if you have it in your Applications folder):

/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --disable-web-security

On a Windows PC, input a command similar to this from a command prompt or from a .bat file:

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --disable-web-security

Note

Obviously this is not a workable solution for a production application. As Electrotank (and other companies who make similar products) continue to improve the functionality of their APIs and add support for HTML5 WebSockets, this limitation should disappear.

The best way to test a multiplayer application on your own development machine is to open two web browsers, or two web browser windows, at the same time. When you look at CH11EX2.html in Google Chrome using this method, you should see something that looks like Figure 11-5.

ElectroServer chat demo on the canvas with JavaScript API

Figure 11-5. ElectroServer chat demo on the canvas with JavaScript API

The full source code is listed in Example 11-2.

Example 11-2. ES5 chat demo

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CH11EX2: ES5 Chat Demo</title>
<script src="modernizr-1.6.min.js"></script>
<script src="ElectroServer-5-Client-JavaScript.js"></script>
<script type="text/javascript">
window.addEventListener("load", eventWindowLoaded, false);

   function eventWindowLoaded () {
      canvasApp();
}

   function canvasSupport () {
       return Modernizr.canvas;
}

   function canvasApp () {

        if (!canvasSupport()) {
          return;
        }

      var theCanvas = document.getElementById("canvasOne");
      var context = theCanvas.getContext("2d");

      var formElement = document.getElementById("sendChat");
      formElement.addEventListener('click', sendMessage, false);

    function drawScreen() {
         //background
         context.fillStyle = "#ffffaa";
         context.fillRect(0, 0, 500, 320);

         context.fillStyle = "#000000";
         context.font = "10px _sans";
         context.textBaseline = "top";

         //box
         context.strokeStyle = '#000000';
         context.strokeRect(5,  5, 490, 310);

         var starty = 15;
         var maxMessages = 22;


         //status box
         context.strokeStyle = '#000000';
         context.strokeRect(345,  10, 145, 285);
         var starti = 0;

         if (statusMessages.length > maxMessages) {
               starti = (statusMessages.length) - maxMessages;

         }
         for (var i = starti;i< statusMessages.length;i++) {
            context.fillText  (statusMessages[i], 350, starty );
            starty+=12;
         }

         //chat box
         context.strokeStyle = '#000000';
         context.strokeRect(10,  10, 335, 285);

         starti = 0;
         lastMessage = chatMessages.length-1;
         if (chatMessages.length > maxMessages) {
                starti = (chatMessages.length) - maxMessages;
         }
         starty = 15;
         for (var i = starti;i< chatMessages.length;i++) {
               context.fillText  (chatMessages[i], 10, starty );
               starty+=12;
         }

         context.fillText  ("User Name:" + username, 10, 295 );

      }

         var statusMessages = new Array();
         var chatMessages = new Array();

         var server = new ElectroServer.Server("server1");

         statusMessages.push(server);

         var availableConnection = new ElectroServer.AvailableConnection 
             ("localhost", 8989, ElectroServer.TransportType.BinaryHTTP);

         server.addAvailableConnection(availableConnection);

         var es = new ElectroServer();
         es.initialize();

         var username;
         var _room;

         es.engine.addServer(server);

         es.engine.addEventListener(MessageType.ConnectionResponse, onConnectionResponse);
         es.engine.addEventListener(MessageType.LoginResponse, onLoginResponse);
         es.engine.addEventListener(MessageType.JoinRoomEvent, onJoinRoomEvent);
         es.engine.addEventListener(MessageType.JoinZoneEvent, onJoinZoneEvent);
         es.engine.addEventListener(MessageType.ConnectionAttemptResponse, 
             onConnectionAttemptResponse);
         es.engine.addEventListener(MessageType.PublicMessageEvent, onPublicMessageEvent);

         es.engine.connect();

         statusMessages.push("Connecting...");

         setInterval(drawScreen, 33);

      function onConnectionAttemptResponse(event) {
            statusMessages.push("connection attempt response!!");
         }

      function onJoinRoomEvent(event) {
            statusMessages.push("joined a room");
            _room = es.managerHelper.zoneManager.zoneById
                (event.zoneId).roomById(event.roomId);
            var formElement = document.getElementById("inputForm");
            formElement.setAttribute("style", "display:true");
         }

      function onJoinZoneEvent(event) {
            statusMessages.push("joined a zone");

         }

      function onConnectionResponse(event) {
            statusMessages.push("Connect Successful?: "+event.successful);
            var r = new LoginRequest();
            r.userName = "CanvasUser_" + Math.floor(Math.random() * 1000);
            es.engine.send(r);
         }

      function onLoginResponse(event) {
            statusMessages.push("Login Successful?: "+event.successful);

            username = event.userName;

            var crr = new CreateRoomRequest();
            crr.zoneName = "TestZoneChat";
            crr.roomName = "TestRoomChat";

            es.engine.send(crr);
         }

      function sendMessage(event) {
            var formElement = document.getElementById("textBox");
            var pmr = new PublicMessageRequest();
            pmr.message = "";
            pmr.roomId = _room.id;
            pmr.zoneId = _room.zoneId;
            var esob = new ElectroServer.EsObject();
            esob.setString("message", formElement.value);
            esob.setString("type","chatmessage");
            pmr.esObject = esob;
            es.engine.send(pmr);
            statusMessages.push("message sent")

         }

      function onPublicMessageEvent(event) {

         var esob = event.esObject;
         statusMessages.push("message received")
         if (esob.getString("type") == "chatmessage") {

            chatMessages.push(event.userName + ":" + esob.getString("message"));

         }

      }

}

</script>
</head>
<body>
<div style="position: absolute; top: 50px; left: 50px;">
<canvas id="canvasOne" width="500" height="320">
 Your browser does not support HTML5 Canvas.
</canvas>
<div id="inputForm"  style="display:none;">
<form>
<input id="textBox" placeholder="your text" />
<input type="button" id ="sendChat" value="Send"/>
</form>
</div>

</div>
</body>
</html>

Further Explorations with ElectroServer

Displaying text on HTML5 Canvas is interesting, but as we have shown you in this book, you can do much more. Let’s add some graphics to the previous demo. We have added a second application for you to peruse, named CH11EX3.html. This application adds the bouncing ball demo app from Chapter 5 to the chat application we just created. It allows chatters to “send” bouncing balls to each other by clicking on the canvas.

The heart of the app is simply another use of the EsObject from the chat application, which is created when the user clicks on the canvas. This EsObject adds information about a ball that one user created for the others in the room:

function eventMouseUp(event) {
   var mouseX;
   var mouseY;
   if (event.layerX ||  event.layerX == 0) { // Firefox
      mouseX = event.layerX ;
      mouseY = event.layerY;
   } else if (event.offsetX || event.offsetX == 0) { // Opera
      mouseX = event.offsetX;
      mouseY = event.offsetY;
   }
   ballcounter++;
   var maxSize = 8;
   var minSize = 5;
   var maxSpeed = maxSize+5;
   var tempRadius = Math.floor(Math.random()*maxSize)+minSize;
   var tempX = mouseX;
   var tempY = mouseY;
   var tempSpeed = maxSpeed-tempRadius;
   var tempAngle = Math.floor(Math.random()*360);
   var tempRadians = tempAngle * Math.PI/ 180;
   var tempvelocityx = Math.cos(tempRadians) * tempSpeed;
   var tempvelocityy = Math.sin(tempRadians) * tempSpeed;
   var pmr = new PublicMessageRequest();
   pmr.message = "";
   pmr.roomId = _room.id;
   pmr.zoneId = _room.zoneId;
   var esob = new ElectroServer.EsObject();
   esob.setFloat("tempX",tempX );
   esob.setFloat("tempY",tempY );
   esob.setFloat("tempRadius",tempRadius );
   esob.setFloat("tempSpeed",tempSpeed );
   esob.setFloat("tempAngle",tempAngle );
   esob.setFloat("velocityx",tempvelocityx );
   esob.setFloat("velocityy",tempvelocityy );
   esob.setString("usercolor",usercolor );
   esob.setString("ballname",username+ballcounter);
   esob.setString("type", "newball");
   pmr.esObject = esob;
   es.engine.send(pmr);
   statusMessages.push("send ball");

   }

When a user connected in the same room receives this public message, we handle the newball event in a similar manner to how we handled the chat text, by using the onPublicMessageEvent() function. When the function sees an event with the type newball, it calls createNetBall(). The createNetBall() function creates ball objects to bounce around the canvas, much like the ones we created in Chapter 5:

function onPublicMessageEvent(event) {
   statusMessages.push("message received")
   var esob = event.esObject;
   if (esob.getString("type") == "chatmessage") {
    chatMessages.push(event.userName + ":" + esob.getString("message"));
   } else if (esob.getString("type") == "newball") {
      statusMessages.push("create ball")
      createNetBall(esob.getFloat("tempX"),esob.getFloat("tempY"),    
          esob.getFloat("tempSpeed"),esob.getFloat("tempAngle"),
          esob.getFloat("tempRadius"),esob.getFloat("velocityx"), 
          esob.getFloat("velocityy"),event.userName,esob.getString("usercolor"),
          esob.getString("ballname") );
   }

}

function createNetBall(tempX,tempY,tempSpeed,tempAngle,tempRadius,tempvelocityx, 
                       tempvelocityy, user, usercolor, ballname) {     
                   
    tempBall = {x:tempX,y:tempY,radius:tempRadius, speed:tempSpeed, angle:tempAngle, 
        velocityx:tempvelocityx, velocityy:tempvelocityy,nextx:tempX, nexty:tempY, 
        mass:tempRadius, usercolor:usercolor, ballname:ballname}
    balls.push(tempBall);
    }

Figure 11-6 shows what this demo looks like when users click the mouse button to send balls to other users. The colors of the balls are assigned randomly.

ElectroServer chat ball demo

Figure 11-6. ElectroServer chat ball demo

Example 11-3 gives the full set of code for CH11EX3.html.

Example 11-3. ES5 ball demo

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>CH11EX3: ES5 Ball Demo</title>
<script src="modernizr-1.6.min.js"></script>
<script src="ElectroServer-5-Client-JavaScript.js"></script>
<script type="text/javascript">
window.addEventListener("load", eventWindowLoaded, false);

function eventWindowLoaded () {
   canvasApp();
}

function canvasSupport () {
     return Modernizr.canvas;
}

function canvasApp () {

      if (!canvasSupport()) {
        return;
      }

      var theCanvas = document.getElementById("canvasOne");
      var context = theCanvas.getContext("2d");

      var formElement = document.getElementById("sendChat");
      formElement.addEventListener('click', sendMessage, false);

        function drawScreen() {
         //background
         context.fillStyle = "#ffffaa";
         context.fillRect(0, 0, 500, 320);

         context.fillStyle = "#000000";
         context.font = "10px _sans";
         context.textBaseline = "top";

         //box
         context.strokeStyle = '#000000';
         context.strokeRect(5,  5, 490, 310);

         var starty = 15;
         var maxMessages = 22;

         //status box
         context.strokeStyle = '#000000';
         context.strokeRect(345,  10, 145, 285);
         var starti = 0;

         if (statusMessages.length > maxMessages) {
             starti = (statusMessages.length) - maxMessages;

         }
         for (var i = starti;i< statusMessages.length;i++) {
            context.fillText  (statusMessages[i], 350, starty );
            starty+=12;
         }

         //chat box
         context.strokeStyle = '#000000';
         context.strokeRect(10,  10, 335, 285);

         starti = 0;
         lastMessage = chatMessages.length-1;
         if (chatMessages.length > maxMessages) {
             starti = (chatMessages.length) - maxMessages;
         }
         starty = 15;
         for (var i = starti;i< chatMessages.length;i++) {
            context.fillText  (chatMessages[i], 10, starty );
            starty+=12;
         }

         context.fillText  ("User Name:" + username, 10, 295 );

         update();
         testWalls();
         render();

      }

          function updateBall(ball) {
            ball.radians = ball.angle * Math.PI/ 180;
            ball.velocityx = Math.cos(ball.radians) * ball.speed;
            ball.velocityy = Math.sin(ball.radians) * ball.speed;

         }

         var statusMessages = new Array();
         var chatMessages = new Array();

         var server = new ElectroServer.Server("server1");

         statusMessages.push(server);

         var availableConnection = new ElectroServer.AvailableConnection
             ("localhost", 8989, ElectroServer.TransportType.BinaryHTTP);

         server.addAvailableConnection(availableConnection);

         var es = new ElectroServer();
         es.initialize();

         var username;
         var usercolor;
         var _room;
         var ballcounter = 0;

         es.engine.addServer(server);

         es.engine.addEventListener(MessageType.ConnectionResponse, onConnectionResponse);
         es.engine.addEventListener(MessageType.LoginResponse, onLoginResponse);
         es.engine.addEventListener(MessageType.JoinRoomEvent, onJoinRoomEvent);
         es.engine.addEventListener(MessageType.JoinZoneEvent, onJoinZoneEvent);
         es.engine.addEventListener(MessageType.ConnectionAttemptResponse, 
             onConnectionAttemptResponse);
         es.engine.addEventListener(MessageType.PublicMessageEvent, onPublicMessageEvent);

         es.engine.connect();


         statusMessages.push("Connecting...");


         var balls = new Array();

         theCanvas.addEventListener("mouseup",eventMouseUp, false);

         var friction = 0;

         setInterval(drawScreen, 33);

         function eventMouseUp(event) {
            var mouseX;
            var mouseY;
            if ( event.layerX ||  event.layerX == 0) { // Firefox
               mouseX = event.layerX ;
               mouseY = event.layerY;
            } else if (event.offsetX || event.offsetX == 0) { // Opera
               mouseX = event.offsetX;
               mouseY = event.offsetY;
            }
            ballcounter++;
            var maxSize = 8;
            var minSize = 5;
            var maxSpeed = maxSize+5;
            var tempRadius = Math.floor(Math.random()*maxSize)+minSize;
            var tempX = mouseX;
            var tempY = mouseY;
            var tempSpeed = maxSpeed-tempRadius;
            var tempAngle = Math.floor(Math.random()*360);
            var tempRadians = tempAngle * Math.PI/ 180;
            var tempvelocityx = Math.cos(tempRadians) * tempSpeed;
            var tempvelocityy = Math.sin(tempRadians) * tempSpeed;
            var pmr = new PublicMessageRequest();
            pmr.message = "";
            pmr.roomId = _room.id;
             pmr.zoneId = _room.zoneId;
            var esob = new ElectroServer.EsObject();
            esob.setFloat("tempX",tempX );
            esob.setFloat("tempY",tempY );
            esob.setFloat("tempRadius",tempRadius );
            esob.setFloat("tempSpeed",tempSpeed );
            esob.setFloat("tempAngle",tempAngle );
            esob.setFloat("velocityx",tempvelocityx );
            esob.setFloat("velocityy",tempvelocityy );
            esob.setString("usercolor",usercolor );
            esob.setString("ballname",username+ballcounter);
            esob.setString("type", "newball");
            pmr.esObject = esob;
            es.engine.send(pmr);
            statusMessages.push("send ball");

         }

         function createNetBall(tempX,tempY,tempSpeed,tempAngle,tempRadius,tempvelocityx, 
                                tempvelocityy, user, usercolor, ballname) {

            tempBall = {x:tempX,y:tempY,radius:tempRadius, speed:tempSpeed, 
                angle:tempAngle, velocityx:tempvelocityx, velocityy:tempvelocityy,  
                nextx:tempX, nexty:tempY, mass:tempRadius, usercolor:usercolor, 
                ballname:ballname}
            balls.push(tempBall);
         }

         function onConnectionAttemptResponse(event) {
            statusMessages.push("connection attempt response!!");
         }

         function onJoinRoomEvent(event) {
            statusMessages.push("joined a room");
            _room = es.managerHelper.zoneManager.zoneById
                (event.zoneId).roomById(event.roomId);
            var formElement = document.getElementById("inputForm");
            formElement.setAttribute("style", "display:true");
         }

         function onJoinZoneEvent(event) {
            statusMessages.push("joined a zone");

         }

         function onConnectionResponse(event) {
            statusMessages.push("Connect Successful?: "+event.successful);
            var r = new LoginRequest();
            r.userName = "CanvasUser_" + Math.floor(Math.random() * 1000);
            es.engine.send(r);
         }

         function onLoginResponse(event) {
            statusMessages.push("Login Successful?: "+event.successful);

            username = event.userName;
            usercolor = "#"+(Math.random()*0xFFFFFF<<0).toString(16);

            var crr = new CreateRoomRequest();
            crr.zoneName = "TestZoneCuesors";
            crr.roomName = "TestRoomCursors";

            es.engine.send(crr);

         }

         function sendMessage(event) {
               var formElement = document.getElementById("textBox");
                var pmr = new PublicMessageRequest();
               pmr.message = "";
               pmr.roomId = _room.id;
                pmr.zoneId = _room.zoneId;
               var esob = new ElectroServer.EsObject();
               esob.setString("message", formElement.value);
               esob.setString("type","chatmessage");
               pmr.esObject = esob;
               es.engine.send(pmr);
               statusMessages.push("message sent")

         }

         function onPublicMessageEvent(event) {
            statusMessages.push("message received")
            var esob = event.esObject;

            if (esob.getString("type") == "chatmessage") {

               chatMessages.push(event.userName + ":" + esob.getString("message"));

            } else if (esob.getString("type") == "newball") {
               statusMessages.push("create ball")
               createNetBall(esob.getFloat("tempX"),esob.getFloat("tempY"), 
                   esob.getFloat("tempSpeed"),esob.getFloat("tempAngle"),
                   esob.getFloat("tempRadius"),esob.getFloat("velocityx"), 
                   esob.getFloat("velocityy"),event.userName,esob.getString("usercolor"), 
                   esob.getString("ballname") );
            }

         }


          function update() {
            for (var i =0; i <balls.length; i++) {
               ball = balls[i];
               //Friction
               ball.velocityx = ball.velocityx - ( ball.velocityx*friction);
               ball.velocityy = ball.velocityy - ( ball.velocityy*friction);

               ball.nextx = (ball.x += ball.velocityx);
               ball.nexty = (ball.y += ball.velocityy);
            }

         }

         function testWalls() {
            var ball;
            var testBall;

            for (var i = 0; i <balls.length; i++) {
               ball = balls[i];

               if (ball.nextx+ball.radius > theCanvas.width) {
                  ball.velocityx = ball.velocityx*-1;
                  ball.nextx = theCanvas.width - ball.radius;

               } else if (ball.nextx-ball.radius < 0 ) {
                  ball.velocityx = ball.velocityx*-1;
                  ball.nextx = ball.radius;

               } else if (ball.nexty+ball.radius > theCanvas.height ) {
                  ball.velocityy = ball.velocityy*-1;
                  ball.nexty = theCanvas.height - ball.radius;

               } else if(ball.nexty-ball.radius < 0) {
                  ball.velocityy = ball.velocityy*−1;
                  ball.nexty = ball.radius;
               }

            }

         }

         function render() {
            var ball;

            for (var i =0; i <balls.length; i++) {

               ball = balls[i];
               ball.x = ball.nextx;
               ball.y = ball.nexty;
               context.fillStyle = ball.usercolor;
               context.beginPath();
               context.arc(ball.x,ball.y,ball.radius,0,Math.PI*2,true);
               context.closePath();
               context.fill();
            }

         }

}


</script>
</head>
<body>
<div style="position: absolute; top: 50px; left: 50px;">
<canvas id="canvasOne" width="500" height="320">
 Your browser does not support HTML5 Canvas.
</canvas>
<div id="inputForm"  style="display:none;">
<form>
<input id="textBox" placeholder="your text" />
<input type="button" id ="sendChat" value="Send"/>
</form>
</div>

</div>
</body>
</html>

This Is Just the Tip of the Iceberg

There is much more you can do with ElectroServer than what we showed you in this chapter. Sending and receiving PublicMessage events can only get you so far when designing multiuser/multiplayer applications.

To start designing multiplayer applications seriously, you will need to delve into the extension and plug-in architecture of ElectroServer, as well as explore plug-in events, which are used to communicate to the server portion of an application. We suggest you check out http://www.electrotank.com/es5.html for more information about the socket server. You can also read ActionScript for Multiplayer Games and Virtual Worlds by Jobe Makar (New Riders). Even though it centers on Flash and an earlier version of ElectroServer, the architectural information about designing apps for a socket server is well worth your time.

At the same time, ElectroServer can be used with technologies other than Canvas (Flash, iOS, etc.), so Canvas will be able to communicate with other socket servers via JavaScript and WebSockets. We chose to base this example on ElectroServer because it allowed us to create a full application for you to test and work through. Other libraries and tools are bound to appear very soon that can work with canvas; for example, the SmartFoxServer Ajax API. Still in beta, this API uses the Google Web Toolkit to connect to the SmartFox socket server through an Ajax connection in JavaScript. This library requires the SmartFoxServer BlueBox add-on module. In theory, this means SmartFoxServer could be used in conjunction with Canvas right now.

The best content for your career. Discover unlimited learning on demand for around $1/day.