2.0.1 • Published 12 months ago
cws v2.0.1
cws
Cross-platform interface for websockets
Install
npm install --save cws
API
For node usage, look at the ws package as that's what's returned.
src/browser.js can either be loaded directly or you can require the module through browserify. Creating a server is not supported in the browser. Arguments to the constructor are passed directly to WebSocket & this module wraps around it to provide a node-style api.
Browser
const CWS = require('cws');
// or use window.CWS
// Connect to a server
const ws = CWS('ws://server/path', /* protocols, */ options);
// Checking the current state
console.log('readyState:', ws.readyState);
// Pass errors to the console
ws.on('error', e => {
console.error(e);
});
// Notify console of closure
ws.on('close', e => {
console.log('The websocket was closed');
});
// Listen for messages & log them
ws.on('message', message => {
if ('string' !== typeof message) throw Error("Message could not be decoded");
const received = JSON.parse(message);
console.log('Message received:', received);
});
// Wait for the socket to open
ws.on('open', () => {
// Say hi
ws.send(JSON.stringify({
type: 'Greeting',
data: 'Hello World',
});
// Close the socket after 5 seconds
setTimeout(() => {
ws.close();
}, 5000);
});
2.0.1
12 months ago
2.0.0
4 years ago
1.2.15
4 years ago
1.2.14
4 years ago
1.2.13
4 years ago
1.2.12
5 years ago
1.2.11
5 years ago
1.2.10
5 years ago
1.2.8
5 years ago
1.2.7
5 years ago
1.2.9
5 years ago
1.2.6
5 years ago
1.2.5
6 years ago
1.2.4
6 years ago
1.2.3
6 years ago
1.2.2
6 years ago
1.2.1
6 years ago
1.2.0
6 years ago
1.1.0
6 years ago
1.0.4
6 years ago
1.0.3
6 years ago
1.0.2
6 years ago
1.0.1
6 years ago
1.0.0
7 years ago
0.2.2
7 years ago
0.2.1
7 years ago
0.2.0
7 years ago
0.1.2
7 years ago
0.1.1
7 years ago
0.1.0
7 years ago
0.0.1
7 years ago