Skip to content

Latest commit

 

History

History
412 lines (272 loc) · 8.38 KB

API.md

File metadata and controls

412 lines (272 loc) · 8.38 KB

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)

  • string bindAddress

    local bound address

This function does things!

// Sample code

function onReceive(peerAddress, message)

  • string peerAddress

    sender peer address

  • object message

    message received from peer

This function does things!

// Sample code

function onClose(error)

  • string error (optional)

    error message, will be undefined if socket closed with function close()

This function does things!

// Sample code

function send(peerAddress, message)

  • string peerAddress

    target peer address

  • object message

    message to send to peer

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)

  • string localAddress

    local bound address

This function does things!

// Sample code

function onReceive(message)

  • object message

    message received from server

This function does things!

// Sample code

function onClose(error)

  • string error (optional)

    error message, will be undefined if socket closed with function close()

This function does things!

// Sample code

function send(message)

  • object message

    message to send to server

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)

  • string listenAddress

    local bound address

This function does things!

// Sample code

function onConnect(clientAddress)

  • string clientAddress

    connected client address

This function does things!

// Sample code

function onReceive(clientAddress, message)

  • string clientAddress

    sender client address

  • object message

    message received from client

This function does things!

// Sample code

function onDisconnect(clientAddress, error)

  • string clientAddress

    disconnected client address

  • string error (optional)

    error message, will be undefined if connection closed with function drop()

This function does things!

// Sample code

function onClose(error)

  • string error (optional)

    error message, will be undefined if socket closed with function close()

This function does things!

// Sample code

function send(clientAddress, message)

  • string clientAddress

    target client address

  • object message

    message to send to client

This function does things!

// Sample code

function drop(clientAddress)

  • string clientAddress

    target client address

This function does things!

// Sample code

function close()

This function does things!

// Sample code