1.0.0 • Published 10 years ago
licy-websocket v1.0.0
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 = require('licy-websocket').WebSocket;
var ws = new WebSocket('ws://localhost:1337', function () {
console.log('ws connection established');
});
ws.on('message', function (event) {
console.log('ws message: ' + event.data);
});
ws.on('close', function () {
console.log('ws closed');
});Install with npm
npm install licy-websocket --saveBrowser 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. Ifprotocolsare specified, they are passed to the underlying implementation. If a callback is given, it is invoked withnullonce the connection was established successfully. If the connection cannot be established, the callback is invoked with anErroras the first argument.ws.send(data): Send data on the websocket.ws.close([code][, reason]): Close the websocket with an optional code and reason. Thecodedefaults to 1000 andreasondefaults to null.ws.destroy(): Callsclose().
Events
message: When a message was received. The only argument is the websocketeventwhich has adataproperty.close: When the websocket was closed. The only argument is the websocketeventwhich has acodeand areasonproperty.error: When an error was received.
Development
npm installto install the dev dependenciesnpm testto lint, run tests on Node and PhantomJS and check code coverage
License
MIT
1.0.0
10 years ago