25.02.2013 Views

Peter Lubbers - Pro HTML 5 Programming

Pro HTML 5 Programming

Pro HTML 5 Programming

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Figure 9-7. The example race website<br />

CHAPTER 9 ■ USING THE <strong>HTML</strong>5 WEB STORAGE API<br />

Each of our web pages will contain a common section to display the leader board data. Each entry in<br />

the leader board will show the name of one of our racers and his or her current distance from the finish<br />

line. When any of our pages is loaded, it will make a WebSocket connection to a race broadcast server<br />

and listen for messages indicating the position of a racer. The racers, in turn, will be sending their<br />

current position to the same broadcast server, causing the position data to stream down to the page in<br />

real time.<br />

All of this has been covered in previous chapters related to Geolocation and WebSockets. In fact,<br />

much of the demonstration code here is shared with the examples from earlier in this book. However,<br />

there is one key difference in this example: when the data arrives in the page, we will store it in the<br />

session storage area for later retrieval. Then, whenever a user navigates to a new page, the stored data<br />

will be retrieved and displayed before making a new WebSocket connection. In this way, the temporary<br />

data is transferred from page to page without using any cookies or web server communication.<br />

Just as we did in the WebSockets chapter, we’ll send our racer location messages across the web in a<br />

simple format that is easy to read and parse. This format is a String that uses the semicolon character (;)<br />

as a delimiter separating the chunks of data: name, latitude, and longitude. For example, a racer named<br />

Racer X who is at latitude 37.20 and longitude –121.53 would be identified with the following string:<br />

;Racer X;37.20;-121.53<br />

Now, let’s dig into the code itself. Each of our pages will contain identical JavaScript code to<br />

connect to the WebSocket server, process and display leader board messages, and save and restore the<br />

leader board using sessionStorage. As such, this code would be a prime candidate to include in a<br />

JavaScript library in a real application.<br />

First, we’ll establish a few utility methods that you’ve seen before. To calculate the distance of any<br />

particular racer from the finish line, we need routines to calculate distance between two geolocation<br />

positions as shown in Listing 9-5.<br />

Listing 9-5. Distance Calculation Routine<br />

// functions for determining the distance between two<br />

// latitude and longitude positions<br />

function toRadians(num) {<br />

return num * Math.PI / 180;<br />

}<br />

225

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!