Buff.js
Translate ArrayBuffers to strings and vice versa in JavaScript. This library is mainly useful for sending WebSocket messages as Binary packets.
Browser
Insert the script tag into your markup:
<script src="https://cdn.jsdelivr.net/npm/buff.js@2.0.13/dist/buff.min.js"></script>
Usage in the browser:
// Load the module
const Buff = require("buff.js");
// Translates the string "test" info an ArrayBuffer.
const buffer = Buff.Write("test");
// Translates the ArrayBuffer back into the string "test".
const string = Buff.Read(buffer);
console.log(string, ' <--> ', buffer);
console.log('# Buffer.js works!');
Node.JS
Install with the command:
npm i buff.js
Usage in Node.js:
// Load the module
const Buff = require("buff.js");
// Translates the string "test" info an ArrayBuffer.
const buffer = Buff.Write("test");
// Translates the ArrayBuffer back into the string "test".
const string = Buff.Read(buffer);
console.log(string, ' <--> ', buffer);
console.log('# Buffer.js works!');
WebSockets Example
Client example:
// Setup the lib for the websocket server.
const Buff = require("buff.js");
let socket = new Object();
socket.setup = function(url) {
let _this = this;
setInterval(function(){
if (!_this.ws || _this.ws.readyState === 3) {
socket.connect(url);
}
},5000);
socket.connect(url);
};
socket.connect = function(url) {
let _this = this;
console.log('[SOCKET]','Connecting to gateway...');
try {
// Create new websocket connection
_this.ws = new WebSocket(url);
// Set binary type so we can send ArrayBuffers
_this.ws.binaryType = 'arraybuffer';
_this.ws.callbacks = new Object();
// Handler for when the client establishes a successful connection with the server.
_this.ws.onopen = function() {
console.log('[SOCKET]','Secure connection established!');
};
// Handler for recieving and processing messages from the server.
_this.ws.onmessage = function(event) {
// Convert the view of the ArrayBuffer into the raw data.
const message = JSON.parse(Buff.Read(event.data));
console.log('[SOCKET]','Received',message);
};
// Handler for when the client looses connection to the server.
_this.ws.onclose = function() {
console.log('[SOCKET]','Connection lost, retrying...');
};
} catch(error) {
console.log('[SOCKET]','Connection lost, retrying...');
}
};
socket.send = function(message) {
let _this = this;
if (!message) return;
if (!_this.ws || _this.ws.readyState === 3) {
console.log('[SOCKET]','Queuing request...');
setTimeout(function(){
socket.send(message);
},5000);
return;
}
// Convert message into binary data (array).
if (typeof message === 'object') message = JSON.stringify(message);
if (typeof message === 'function') return;
console.log('[SOCKET]','Sent',message);
_this.ws.send(Buff.Write(message));
};
socket.connect('ws://127.0.0.1:8080');
Server example:
const WebSocket = require('ws');
const Buff = require('buff.js');
const path = require('path');
const fs = require('fs');
const wss = new WebSocket.Server({
port: 8080
});
let helpers = {
random: function(length) {
let text = '',
possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
for (let i = 0; i < (length || 10); i++)
text += possible.charAt(Math.floor(Math.random()*possible.length));
return text;
}
};
wss.on('connection',function(socket){
socket.on('message',function(data){
// Convert back to raw data.
const message = Buff.Read(data);
// Echo the message back to the sender and compare the raw data and ArrayBuffer.
socket.send(data);
console.log('"'+message+'"',' <--> ',data);
});
});