licy-websocket.js
Licy wrapper for a WebSocket based on the websocket module.
Features
- Async creation: Function calls are automatically deferred until constructor invokes callback
- All function calls are observable and can be intercepted
- Create hierarchies of instances that get destroyed together
Usage
var WebSocket = WebSocket; var ws = 'ws://localhost:1337' { console;};ws;ws;
Install with npm
npm install licy-websocket --save
Browser support
Use Browserify to create a standalone file. The test suite passes on IE 10, 11, Chrome *, Filefox * and PhantomJS.
API
var ws = new WebSocket(url[, protocols][, callback])
: Creates a new W3C websocket and connect to the given URL. Ifprotocols
are specified, they are passed to the underlying implementation. If a callback is given, it is invoked withnull
once the connection was established successfully. If the connection cannot be established, the callback is invoked with anError
as the first argument.ws.send(data)
: Send data on the websocket.ws.close([code][, reason])
: Close the websocket with an optional code and reason. Thecode
defaults to 1000 andreason
defaults to null.ws.destroy()
: Callsclose()
.
Events
message
: When a message was received. The only argument is the websocketevent
which has adata
property.close
: When the websocket was closed. The only argument is the websocketevent
which has acode
and areason
property.error
: When an error was received.
Development
npm install
to install the dev dependenciesnpm test
to lint, run tests on Node and PhantomJS and check code coverage
License
MIT