websocket-libxwsj
v3.3.3
Published
This is a lightweight WebSocket library for Node.
Downloads
6
Maintainers
Readme
Node WebSocket Library
This is a lightweight WebSocket library for Node.
Installation
$ npm install websocket-lib
Documentation
Examples
Server
var ws = require('websocket-lib');
var server = ws.createServer(function (session) {
console.log('client connected');
session.on('data', function (data) {
console.log('client msg:', data);
this.send('Hi, ' + data);
});
session.on('close', function () {
console.log('session closed');
});
});
server.listen(8000);
Client
var ws = require('websocket-lib');
var client = ws.connect('ws://localhost:8000', function (session) {
session.setEncoding('utf8');
session.send('Client');
session.on('data', function (data) {
console.log('server msg:', data);
});
session.on('close', function () {
console.log('session closed');
});
});
Browser
var ws = new WebSocket('ws://localhost:8000');
ws.onmessage = function (event) {
console.log('server msg:', event.data);
};
ws.onclose = function () {
console.log('session closed');
};
ws.onopen = function () {
ws.send('Client');
};