25.02.2013 Views

Peter Lubbers - Pro HTML 5 Programming

Pro HTML 5 Programming

Pro HTML 5 Programming

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

234<br />

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

notifications. In the second window, open the racerBroadcast.html file. After this page, too, has<br />

connected to the WebSocket broadcast site, enter a name for your racer, and click the Start button. You’ll<br />

see that the racer broadcast has transmitted the location of your favorite racer, and it should show up in<br />

the leader board in your other browser window. Figure 9-9 shows what this looks like.<br />

Figure 9-9. Race page and racerBroadcast.html side by side<br />

Now, navigate to other racing club pages using the Signup and About the Race links on the left side<br />

of the page. Because all of these pages have been configured to load our script, they will immediately<br />

load and populate the leader board with the previous racer data, which was delivered while browsing<br />

other pages. Send more racer status notifications (from the broadcast page), and you’ll see them<br />

propagate through the club site pages as you navigate, as well.<br />

Now that we’ve finished our code, let’s review what we’ve built. We’ve created a simple function<br />

block, suitable for inclusion in a shared JavaScript library, which connects to a WebSocket broadcast<br />

server and listens for racer updates. When an update is received, the script displays the position in the<br />

page and stores it using <strong>HTML</strong>5 sessionStorage. When the page is loaded, it checks for any previously<br />

stored racer position values, thus maintaining the state as the user navigates the site. What are some of<br />

the benefits we gain from this approach?<br />

• Reduced network traffic: Race information is stored locally in the browser. Once it<br />

arrives, it sticks around for every page load, rather than using cookies or server<br />

requests to fetch it again.<br />

• Immediate display of values: The browser pages themselves can be cached rather<br />

than loaded from the network, because the dynamic parts of the page—the<br />

current leaderboard status—are local data. This data is rapidly displayed without<br />

any network load time.

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

Saved successfully!

Ooh no, something went wrong!