Welcome to Multiplayer Game Development with HTML5. This book will teach you how to develop games that support interacting multiple players in the same game world, and how to perform network programming operations in order to implement such systems. It covers topics such as WebSockets and client-side and server-side game programming in JavaScript with Node.js, latency reduction techniques, and handling server queries from multiple users. We will accomplish this by walking you through the process of developing two actual multiplayer games from start to finish, and it will also teach you about various topics in HTML5 game development in the process. The aim of the book is to teach you to create game worlds for multiple players who want to compete or interact through the Internet using HTML5.
Chapter 1, Getting Started with Multiplayer Game Programming, introduces network programming, with emphasis on designing a multiplayer game. It illustrates the basic concepts of multiplayer game development by walking you through the creation of a real-time game of Tic Tac Toe.
Chapter 2, Setting Up the Environment, describes the current state of the art in the JavaScript development world, including JavaScript in the server through Node.js. It also describes the current techniques to manage the development cycle in JavaScript with workflow and resource management tools, such as Npm, Bower, Grunt, and more.
Chapter 3, Feeding a Snake in Real Time, takes an existing single-player Snake game and builds in the ability to play with multiple players in the same game world using the tools heretofore described. Concepts of lobby, rooms, matchmaking, and handling queries from users are also described and demonstrated, which add the functionality to the Snake game. The chapter introduces the most powerful and widely used WebSocket abstraction in the industry today—socket.io.
Chapter 4, Reducing Network Latency, teaches you techniques to reduce network latency in order to create a smooth playing experience. The most common among such techniques—client prediction—is demonstrated and incorporated into the Snake game that is described in the previous chapter. The game server code is also updated for the purpose of performance by introducing a second update loop.
Chapter 5, Leveraging the Bleeding Edge, describes the exciting opportunities that are found in game development on the web platform. It explains WebRTC, HTML5's gamepad, the fullscreen mode, and media capture APIs. Other promised and experimental technologies and APIs are also described here.
Chapter 6, Adding Security and Fair Play, covers common flaws and security vulnerabilities that are associated with network gaming. Here, common techniques are described and demonstrated, allowing you to develop games that provide a playing experience that is free from cheating.
To use this book, you will need a working installation of Node.js and Npm, a modern web browser (such as Google Chrome 5.0, Firefox 3.5, Safari 5.0, or Internet Explorer 9.0 and their later versions), and a text editor or an integrated development environment (IDE). You will also need basic to intermediate JavaScript knowledge as well as some previous game programming experience, preferably in JavaScript and HTML5.
This book is targeted at HTML5 game developers who can make basic single player games and would now like to learn how to incorporate multiplayer functionality in their HTML5 games as quickly as possible.
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: " The first will be keyed with the value of action
and the second will have a key of data
."
A block of code is set as follows:
wss.on('connection', function connection(ws) { board.on(Board.events.PLAYER_CONNECTED, function(player) { wss.clients.forEach(function(client) { board.players.forEach(function(player) { client.send(makeMessage(events.outgoing.JOIN_GAME, player));
When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:
validator.isEmail('[email protected]'); //=> true validator.isBase64(inStr); validator.isHexColor(inStr); validator.isJSON(inStr);
Any command-line input or output is written as follows:
npm install socket.io --save npm install socket.io-client –save
Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail <[email protected]>
, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
You can download the example code files from your account at http://www.packtpub.com for all the Packt Publishing books you have purchased. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/3109OS_ Graphics.pdf.
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at <[email protected]>
with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
If you have a problem with any aspect of this book, you can contact us at <[email protected]>
, and we will do our best to address the problem.