Game Development Reference
In-Depth Information
Chapter 9
A Real-Time Multiplayer Game
Using WebSockets
In this chapter, we will walk through the creation of a simple multiplayer game. We will use the HTML5
canvas to display graphics to the user, WebSockets to facilitate communication with our server, and
node.js to create the server.
WebSockets are truly a boon to game development on the web. Before the advent of WebSockets, game
clients had to speak to the game's server by polling with XMLHttpRequest or by using Flash. The former
has many drawbacks—particularly that it is very wasteful of bandwidth, and the latter is something that we,
as HTML5 developers, should like to avoid. WebSockets, on the other hand, are native to the browser,
with a simple JavaScript interface. Data sent over WebSockets doesn't carry HTTP headers, so it is much
more efficient than XMLHttpRequest .
The WebSocket protocol has gone through several iterations since 2010, but now appears to be
converging to a final version. Browser support for WebSockets has been notoriously spotty, but the
September 2011 releases of Chrome 14 and Firefox 7, both supporting the same version (“hybi-10”) of the
WebSocket protocol, are a good sign that things are settling down. Internet Explorer 10, not yet released
at the time of this writing, will also implement that version. Browser support will only improve from here.
WebSockets are easy to implement and extremely useful. Let us begin, then, to develop an online bumper
car game!
 
Search WWH ::




Custom Search