HTML and CSS Reference
In-Depth Information
The WebSocket constructor can also include an array of protocol names that the
client supports, which lets the server decide which one to use. Listing 2-3 shows a sample
WebSocket constructor with a list of protocols it supports, represented as an array:
Listing 2-3. Sample WebSocket Constructor with Protocol Support
// Connecting to the server with multiple protocol choices
var echoSocket = new
WebSocket(" ws:// " , [ "com.kaazing.echo",
"example.imaginary.protocol" ])
echoSocket.onopen = function(e) {
// Check the protocol chosen by the server
console.log( echoSocket.protocol );
In Listing 2-3, because the WebSocket server at ws:// only
understands the com.kaazing.echo protocol and not example.imaginary.protocol , the
server chooses the com.kaazing.echo protocol when the WebSocket open event fires.
Using an array gives you flexibility in enabling your application to use different protocols
with different servers.
We'll discuss the WebSocket Protocol in depth in the next chapter, but in essence,
there are three types of protocols you can indicate with the protocols argument:
Registered protocols: Standard protocols that have been
officially registered according to RFC 6455
(The WebSocket Protocol) and with the IANA (Internet
Assigned Numbers Authority), the official governing body for
registered protocols. An example of a registered protocol is
Microsoft's SOAP over WebSocket protocol. See
for more information.
Open protocols: Widely used and standardized protocols like
XMPP and STOMP, which have not been registered as official
standard protocols. We will examine how to use these types of
protocols with WebSocket in the subsequent chapters.
Custom protocols: Protocols that you've written and want to use
with WebSocket.
In this chapter, we focus on using the WebSocket API as you would for your own
custom protocol and examine using open protocols in the later chapters. Let's take a look
at the events, objects, and methods individually and put them together into a working
Search WWH ::

Custom Search