Book Image

PhantomJS Cookbook

By : Rob Friesel
Book Image

PhantomJS Cookbook

By: Rob Friesel

Overview of this book

Table of Contents (15 chapters)
PhantomJS Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Working with WebSockets in PhantomJS


This recipe discusses how to open and work with a WebSocket connection in PhantomJS.

Getting ready

To run this recipe, we will need a script that requests a host that also exposes WebSocket connections.

Note

Although PhantomJS version 1.9 does have WebSocket support, that support is limited to the hixie-76 draft of the protocol. PhantomJS 2.0 is scheduled to include the more modern RFC 6455 version of WebSockets. When writing scripts for PhantomJS that intend to use WebSocket connections, we must ensure that the server supports the hixie-76 version of the protocol.

Tip

A good introduction to the WebSocket protocol and the HTML5 API can be found at http://www.websocket.org/aboutwebsocket.html.

The script in this recipe is available in the downloadable code repository as recipe14.js under chapter03. If we run the provided example script, we must change to the root directory for the book's sample code.

Lastly, the script in this recipe runs against the demo site...