1.5.0 • Published 5 years ago
sudpee v1.5.0
__
/\ \
____ __ __ \_\ \ _____ __ __
/',__\/\ \/\ \ /'_` \/\ '__`\ /'__`\ /'__`\
/\__, `\ \ \_\ \/\ \L\ \ \ \L\ \/\ __//\ __/
\/\____/\ \____/\ \___,_\ \ ,__/\ \____\ \____\
\/___/ \/___/ \/__,_ /\ \ \/ \/____/\/____/
\ \_\
\/_/ Simple UDP for node.
Features
- Sending and receiving unicast/broadcast messages via UDP
- Automatically stringifies and parses sent and received JSON
- CLI Tool
Documentation
Usage
Receving Data
const receiver = await sudpee.receive((message, info) => {}, [port], [address])messagereceived message (if string is JSON, it is automatically parsed to JSON)info{ receiverAddress: string, receiverPort: number, senderAddress: string, senderPort: number, size: number, }porton which port to listen for data (default: 2020)addresson which address to listen for data (default: 0.0.0.0)receiver.finish()close underlying socketreceiver.on('error', err => {})callback for errors from the receiver
Sending Data
await sudpee.send(message, [port], [address])messagethe message to be sentportto which port to send (default: 2020)addressto which address to send (default=broadcast: 255.255.255.255)
Basic Example
import * as sudpee from 'sudpee';
(async () => {
// receive UDP broadcast on port 3000
await sudpee.receive(msg => console.log(msg), 3000)
// send UDP broadcast to port 3000
sudpee.send('Hello World', 3000)
})()$ Hello WorldAdvanced Example
import * as sudpee from 'sudpee';
(async () => {
const end = 3
const receiver = await sudpee.receive((msg, info) => {
console.log('message: ', msg, `from: ${info.senderAddress}:${info.senderPort}`)
// automatically parses JSON
// .finish() closes the underlying socket
if (msg.counter === end) receiver.finish()
}, 3000, '192.168.1.23')
receiver.on('error', err => console.error(err))
for (let i = 1; i <= end; i++) {
// automatically stringifies JSON
await sudpee.send({ counter: i }, 3000, '192.168.1.23')
}
})()$ message: { counter: 1 } from: 192.168.1.23:37357
$ message: { counter: 2 } from: 192.168.1.23:58838
$ message: { counter: 3 } from: 192.168.1.23:34600CLI Tool
Install CLI
npm i sudpee -g
Receving Data with CLI
sudpee receive <options>
-pport (default: 2020)-aaddress (default: 0.0.0.0)-tprint timestamp-sprint sender address--helpprint help
Sending Data with CLI
sudpee send <options>
-pPort (default: 2020)-aaddress (default=broadcast: 255.255.255.255)-mthe message to be sent-iread message from stdin (alternative to-m)--helpprint help