Coder Social home page Coder Social logo

adnl-js's Introduction

adnl

npm GitHub top language

ADNL JavaScript implementation.

How to install

npm i adnl

Simple usage

import {
    ADNLClientTCP, // Not available for browser context
    ADNLClientWS
} from 'adnl'

const ADNL_HOST = '127.0.0.1'
const ADNL_PORT = 1337
const ADNL_PUB_KEY = 'hex/base64/buffer'
const URL = `tcp://${ADNL_HOST}:${ADNL_PORT}`
const URL_PROXY = `wss://proxy.example.com/?dest_host=${ADNL_HOST}:${ADNL_PORT}`

const TL_GETTIME = '7af98bb435263e6c95d6fecb497dfd0aa5f031e7d412986b5ce720496db512052e8f2d100cdf068c7904345aad16000000000000'
const TL_PARSE_GETTIME = (data: Buffer) => {
    const unix = data.slice(data.byteLength - 7, data.byteLength - 3).readUint32LE(0)

    return new Date(unix * 1000).toString()
}

const clientTCP = new ADNLClientTCP(URL, ADNL_PUB_KEY)
    .on('connect', () => console.log('on connect'))
    .on('close', () => console.log('on close'))
    .on('data', (data: Buffer) => console.log('on data:', TL_PARSE_GETTIME(data)))
    .on('error', (error: Error) => console.log('on error:', error))
    .on('ready', () => {
        console.log('on ready')

        let counter = 0
        let interval = setInterval(() => {
            client.write(Buffer.from(TL_GETTIME, 'hex'))
        
            if (++counter === 5) {
                clearInterval(interval)
                client.end()
            }
        }, 3000)
    })

await clientTCP.connect()

// OR

const clientWSProxy = new ADNLClientWS(URL_PROXY, ADNL_PUB_KEY)
    .on('connect', () => console.log('on connect'))
    .on('close', () => console.log('on close'))
    .on('data', (data: Buffer) => console.log('on data:', TL_PARSE_GETTIME(data)))
    .on('error', (error: Error) => console.log('on error:', error))
    .on('ready', () => {
        console.log('on ready')

        let counter = 0
        let interval = setInterval(() => {
            client.write(Buffer.from(TL_GETTIME, 'hex'))
        
            if (++counter === 5) {
                clearInterval(interval)
                client.end()
            }
        }, 3000)
    })

await clientWSProxy.connect()

License

MIT License

adnl-js's People

Contributors

tjifyodor avatar truecarry avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.