0.1.0 • Published 2 years ago

typescript-udptool v0.1.0

Weekly downloads
-
License
BSD-3-Clause
Repository
github
Last release
2 years ago

Udp Tool

Used to broadcast IPv4 UDP data.

travis-img codecov-img github-issues-img contrib-welcome-img license-img


Installation

npm i @haukeschulz/typescript-udptool

Usage

A small example code snippet showing possible use cases.

import { broadcast } from '@haukeschulz/typescript-udptool'

/* Use a Buffer or just the plain string */
const data: Buffer = Buffer.from('This is my message')

/* Will broadcast the data to 255.255.255.255:65432 */
broadcast(data, 65432)

/* Use another address */
broadcast(data, 65432, '127.0.0.1')

/* Example of a callback function used for the broadcast */
const myCallbackHandler = (error: Error | null, bytes: number): void => {

    /* Error is hopefully null */
    if (error) {
        console.error(error.message)
    }

    /* The number of bytes sent should equal the buffer length in this case */
    console.log('Sent ' + bytes + '/' + data.length + ' bytes')
}

/*
 * Broadcast to 255.255.255.255:65432 and provide possible errors and
 * the bytes sent to the callback function.
 */
broadcast(data, 65432, myCallbackHandler)

/* Finally use the broadcast with all possible parameters */
broadcast(data, 65432, '10.11.12.13', myCallbackHandler)