HTML and CSS Reference
In-Depth Information
To simply extend the WebSocket Protocol, there is another mechanism, called
Like protocols, extensions are negotiated with a Sec- header. The connecting client
sends a Sec-WebSocket-Extensions header containing the names of the extension
(or extensions) it supports.
While you cannot negotiate more than one protocol at a time, you can negotiate
more than one extension at a time.
For example, Chrome might send the following header to indicate that it will accept
an experimental compression extension:
Sec-WebSocket-Extensions: x-webkit-deflate-frame
Extensions are so named because they extend the WebSocket Protocol. Extensions
can add new opcodes and data fields to the framing format. You may find it more difficult
to deploy a new extension than a new protocol (or “subprotocol”) because browser
vendors must explicitly build in support for these extensions. You'll probably find it much
easier to write a JavaScript library that implements a protocol than to wait for all browser
vendors to standardize an extension and all users to update their browsers to the version
supporting that extension.
Writing a WebSocket Server in JavaScript
with Node.js
Now that we've examined the essentials of the WebSocket Protocol, let's step through
writing our own WebSocket server. There are many existing implementations of the
WebSocket Protocol; you may choose to use an existing implementation in your
applications. However, you may need to write a new server or modify an existing server
out of necessity or just because you can. Writing your own implementation of the
WebSocket Protocol can be fun and illuminating, and can help you understand and
evaluate other servers, clients, and libraries. Best of all, it can give you new insights on
networking, communication, and the Web.
The example server in this chapter is written in JavaScript using the IO APIs provided
by Node.js. We chose these technologies simply to limit the code samples in this topic to
a single language. Since you are likely using JavaScript with HTML5 for your front-end
development, there is a good chance you will be able to read this code fluently, as well.
Of course, it isn't necessary that you write your server in JavaScript, and there are strong
reasons why you might choose another language. The fact that WebSocket is a language-
agnostic protocol means you can select any programming language capable of listening
on a socket and create a server.
Search WWH ::

Custom Search