Socketify

API Documentation

Contents

function uuidv4

Takes no parameter, generates and returns UUIDv4↗ string back.

var coolID = uuidv4();
console.log(`my cool universally unique ID is ${coolID}`);

object socketify

This object being injected by content script at document start event and exposes socket creation functions to window.

function udpPeer(bindAddress, handlers)

Creates UDP socket, binds to specified address and fires onOpen or onClose event afterwards.

var myPeer = socketify.udpPeer(":9696", {
    onOpen: function (address) {
        console.log(`peer bound to <${address}>`);
    },
    onReceive: function (address, message) {
        console.log(`peer received <${address}>: ${message}`);
    },
    onClose: function (error) {
        if (error) {
            console.log(`peer closed with error: ${error}`);
        } else {
            console.log(`peer closed`);
        }
    }
});

function tcpClient(serverAddress, handlers)

Creates TCP socket, opens connection to server with specified address and fires onOpen or onClose event afterwards.

var myClient = socketify.tcpClient("127.0.0.1:9696", {
    onOpen: function (address) {
        console.log(`client bound to <${address}> and connected`);
    },
    onReceive: function (message) {
        console.log(`client received: ${message}`);
    },
    onClose: function (error) {
        if (error) {
            console.log(`client closed with error: ${error}`);
        } else {
            console.log(`client closed`);
        }
    }
});

function tcpServer(listenAddress, handlers)

Creates TCP socket, starts listening on specified address and fires onOpen or onClose afterwards.

var myServer = socketify.tcpServer(":9696", {
    onOpen: function (address) {
        console.log(`server bound to <${address}> and listening`);
    },
    onConnect: function (address) {
        console.log(`server connected to <${address}>`);
    },
    onReceive: function (address, message) {
        console.log(`server received <${address}>: ${message}`);
    },
    onDisconnect: function (address, error) {
        if (error) {
            console.log(`server disconnected from <${address}> with error: ${error}`);
        } else {
            console.log(`server disconnected from <${address}>`);
        }
    },
    onClose: function (error) {
        if (error) {
            console.log(`server closed with error: ${error}`);
        } else {
            console.log(`server closed`);
        }
    }
});

object udpPeer

UDP socket instance on browser that bridges native calls.

string id

Unique socket id assigned by window to specify instance, primarly used to dispatch calls.

// Sample code

function onOpen(bindAddress)

This function does things!

// Sample code

function onReceive(peerAddress, message)

This function does things!

// Sample code

function onClose(error)

This function does things!

// Sample code

function send(peerAddress, message)

This function does things!

// Sample code

function close()

This function does things!

// Sample code

object tcpClient

TCP client socket instance on browser that bridges native calls.

string id

Unique socket id assigned by window to specify instance, primarly used to dispatch calls.

// Sample code

function onOpen(localAddress)

This function does things!

// Sample code

function onReceive(message)

This function does things!

// Sample code

function onClose(error)

This function does things!

// Sample code

function send(message)

This function does things!

// Sample code

function close()

This function does things!

// Sample code

object tcpServer

TCP server socket instance on browser that bridges native calls.

string id

Unique socket id assigned by window to specify instance, primarly used to dispatch calls.

// Sample code

function onOpen(listenAddress)

This function does things!

// Sample code

function onConnect(clientAddress)

This function does things!

// Sample code

function onReceive(clientAddress, message)

This function does things!

// Sample code

function onDisconnect(clientAddress, error)

This function does things!

// Sample code

function onClose(error)

This function does things!

// Sample code

function send(clientAddress, message)

This function does things!

// Sample code

function drop(clientAddress)

This function does things!

// Sample code

function close()

This function does things!

// Sample code