HTML5 and CSS3, Second Edition

(singke) #1
setupChat() method into the complete() callback. The complete() callback runs at
the very end of the loading process, regardless of the outcome of the test. It’s
the perfect place for code that we always have to run.

With that in place, our chat application will work in all major browsers, provided
that the server hosting your chat server also serves a Flash Socket Policy file.

Flash Socket Policy What?
For security purposes, Flash Player will communicate via sockets only with
servers that allow connections to Flash Player. Flash Player attempts to
retrieve a simple XML file called the Flash Socket Policy file on port 843, and
then on the same port your server uses. The player sends a request containing
this data:

<policy-file-request/>

It expects the server to return a response like this:


<cross-domain-policy>
<allow-access-fromdomain="*"to-ports="*"/>
</cross-domain-policy>

This is a very generic policy file that allows everyone to connect to this service.
You’d want to specify the policy to be more restrictive if you were working
with more sensitive data. Just remember that you have to serve this file from
the same IP address as your Web Sockets server, on either the same port or
port 843. It’s best if you can serve it from port 843, because Flash Player
always sends a request there first.

The sample web server in the example code for this book contains a simple
Flash Socket Policy server, so you can try this code out. See Node.js and the
Example Server, on page xv, for instructions on setting up Node.js and the
sample servers for this book. Once the server is running, you should be able
to play with the completed chat server at http://localhost:8000/html5_web-
sockets/index.html. For extra fun, connect to it from Internet Explorer 8 and
Chrome, as in Figure 35, Cross-browser chatting, on page 226.

If you’re using virtual machines for testing, make sure you use the IP address
of the computer running the server, as localhost won’t work.

We can’t go through it in detail here, but know that you should explore the
code for the chat server, too. You’ll find it in the example code in lib/chat.js.

Chat servers are just the beginning. With Web Sockets, we finally have a
robust yet simple way to push data to our visitors’ browsers in real time.

Next we’ll use the web browser to determine our latitude and longitude.


report erratum • discuss

Chatting with Web Sockets • 225


Download from Wow! eBook <www.wowebook.com>

Free download pdf