Skip to content

Latest commit

 

History

History
50 lines (32 loc) · 1.91 KB

W3CWebSocket.md

File metadata and controls

50 lines (32 loc) · 1.91 KB

W3CWebSocket

var W3CWebSocket = require('websocket').w3cwebsocket

Implementation of the W3C WebSocket API for browsers.

The exposed class lets the developer use the browser W3C WebSocket API in Node:

var WS = require('websocket').w3cwebsocket;

WS === window.WebSocket
// => true when in the browser

var ws = new WS('ws://example.com/resource', 'foo', 'http://example.com');
// - In Node it creates an instance of websocket.W3CWebSocket.
// - In the browser it creates an instance of window.WebSocket (third parameter
//   is ignored by the native WebSocket constructor).

ws.onopen = function() { console.log('ws open'); };
// etc.

Constructor

new W3CWebSocket(requestUrl, requestedProtocols, [[[[origin], headers], requestOptions], clientConfig])

clientConfig is the parameter of the WebSocketClient constructor.

requestUrl, requestedProtocols, origin, headers and requestOptions are parameters to be used in the connect() method of WebSocketClient.

This constructor API makes it possible to use the W3C API and "browserify" the Node application into a valid browser library.

When running in a browser (for example by using browserify) the browser's native WebSocket implementation is used, and thus just the first and second arguments (requestUrl and requestedProtocols) are used (those allowed by the W3C WebSocket API).

Limitations

  • bufferedAmount attribute is always 0.
  • binaryType is "arraybuffer" by default given that "blob" is not supported (Node does not implement the Blob class).
  • send() method allows arguments of type DOMString, ArrayBuffer, ArrayBufferView (Int8Array, etc) or Node Buffer, but does not allow Blob.