This recipe discusses how to open and work with a WebSocket connection in PhantomJS.
To run this recipe, we will need a script that requests a host that also exposes WebSocket connections.
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.
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 ...