HTML and CSS Reference
In-Depth Information
Chapter 11
Introduction to WebSockets
for Game Developers
Peter Lubbers, Program Manager, Google Developer Relations
It is a bit hard to imagine, but the Web wasn't always as dynamic as it is today. Before I discuss WebSockets, let's take a
trip down memory lane.
Before Asynchronous JavaScript and XML (AJAX) and its cornerstone, XMLHttpRequest (XHR), gained
widespread industry adoption, web application updates could only be achieved by refreshing a page. AJAX came
of age in the early 2000s and represented the first wave of interactive tools, giving developers the ability to make
applications feel more like their desktop counterparts. An AJAX call could be fired off to retrieve the current stock
price, update shipping costs, or validate form fields, all without a full-page reload. In addition to AJAX, this need for
dynamic data started to be served by what is commonly referred to as Comet or AJAX push . Comet simulates real-time
interactivity by keeping a connection open forever, or at least for a reasonably long time. There are various methods of
doing this, including having a hidden persistent inline frame (Iframe) (“forever frame”), long polling, or using Java or
Flash plug-ins.
When Comet first became popular, circa 2006, many browsers had a maximum of two concurrent connections,
so development was tricky if you wanted to emulate real-time events but needed to draw from many sources. In the
quest for page load speed, most modern browsers have significantly raised their concurrent connection limits. This
is not to say that you have carte blanche with Comet; it still has the overhead of being transported through HTTP, so
there are a lot of extraneous HTTP header data being generated each time, even though the destination URL
doesn't change.
The WebSocket protocol is a fully bidirectional data transport mechanism over a Transmission Control Protocol
(TCP) connection, and it can traverse proxies. The Internet Engineering Task Force (IETF) codified its standard, RFC
6455, in 2011. WebSockets can be seen as an evolution of the tools used to deliver and respond to real-time events.
AJAX and Comet implement a more traditional request-response model that is somewhat symmetrical, whereas
a WebSocket, after establishing the connection, is more asymmetrical—basically, “don't call us, we'll call you.”
WebSocket connectivity is present in most modern browsers and mobile platforms.
As applications such as online games become less tolerant of latency, overhead becomes a problem. WebSockets
are great for gaming because it provides a means of achieving real-time multiplayer interaction with greatly
reduced overhead.
Setting Up a WebSocket Connection
A WebSocket connection begins with a handshake from the client to the server. The client initiates it over HTTP with
a randomly selected Base64-encoded Sec-WebSocket-Key and other data on the desired connection. This all happens
under the hood. A sample client connection request is shown in Listing 11-1.
Search WWH ::

Custom Search