1.0.3 • Published 5 years ago

@codelix/xws-browser v1.0.3

Weekly downloads
-
License
MIT
Repository
-
Last release
5 years ago

XWS-Browser

A WebSocket wrapper for the browser, that supports auto reconnection, and integrated packet management.


Installation

npm install --save @codelix/xws-browser

Then include the bundle.js in the root.


Usage

// Define a config
let config = {
	shouldReconnect: true,			// Should try and reconnect
	reconnectInterval: 2000,		// How long until it tries to reconnect
	maxReconnectTries: false,		// How many times it should try and reconnect, false = unlimited
	debug: true,					// Whether to debug, this adds a basic console logging to thrown error listener
}

// Create instance
const socket = new XWSBrowser(/* config */);

// Attach a listener
socket.addListener(listenerType: string, callback: any);

// Remove a listener
socket.removeListener(listenerId: number)

// Connect to endpoint
socket.connect(api_uri: string);

// Will send a command that is in packet format
socket.send(command: string, content: any);

// Will send a basic plain normal message
socket.sendRaw(message: string);

// Will disconnect the socket and not retry
socket.disconnect();

// Will set the packet read or create function (has default of command: string, content: Object, timestamp: string)
socket.setPacketFunction(packet_read|packet_create, func);

Example

// Define a config
let config = {
	shouldReconnect: true,
	reconnectInterval: 2000,
	maxReconnectTries: false,
	debug: true,
}

// Create instance
const socket = new XWSBrowser(config);

// Attach on connection opened event
let listenerId1 = socket.addListener('onopen', (event) => {
	console.log('Connected');
	console.log(event);
});

// Attach on connection closed event
let listenerId2 = socket.addListener('onclose', (event) => {
	console.log('Closed');
	console.log(event);
});

// Attach on error event
let listenerId3 = socket.addListener('onerror', (event) => {
	console.log('Error');
	console.log(event);
});

// Attach an event for the on message (returns packet)
let listenerId4 = socket.addListener('onmessage', (packet) => {
	console.log('Message');
	console.log(packet);
});

// Attach an event for the on message (returns string)
let listenerId5 = socket.addListener('onmessageRaw', (message) => {
	console.log('Raw Message');
	console.log(message);
});

// Attach an event for on reconnect
let listenerId6 = socket.addListener('onreconnect', (event) => {
	console.log('Reconnected');
	console.log(event);
});

// Attach an event for all logging from socket information
let listenerId7 = socket.addListener('onlog', (event) => {
	console.log('Log');
	console.log(event);
});

// Attach an event for all caught thrown errors
let listenerId8 = socket.addListener('onthrownerror', (err) => {
	console.log('On thrown error');
	console.log(err);
});

// Remove the onlog listener
socket.removeListener(listenerId7);

Set the on packet create function
socket.setPacketFunction('packet_create', (command, content) => {
	let tstamp: number = new Date().valueOf();
	let packet: any = {
		command: command,
		content: content,
		timestamp: tstamp,
	}
	return JSON.stringify(packet);
});

// Set the on packet read function
socket.setPacketFunction('packet_read', (packet) => {
	return JSON.parse(packet);
});

// Connect to endpoint
socket.connect('wss://echo.websocket.org');

// Will send a command that is in packet format
socket.send('testCommand', {some: 'data'});

// Will send a basic plain normal message
socket.sendRaw('Hello, World!');

// Will disconnect the socket and not retry
socket.disconnect();
1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago