Skip to content

Lanfei/websocket-lib

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

94 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Node WebSocket Library

This is a lightweight WebSocket library for Node.

NPM

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');
};

About

A lightweight WebSocket library for Node.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published