-
Notifications
You must be signed in to change notification settings - Fork 0
/
index-cli.js
executable file
·91 lines (85 loc) · 2.36 KB
/
index-cli.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#!/usr/bin/env node
import yargs from 'yargs'
import readline from 'node:readline'
import * as sudpee from './index.js'
const DEFAULT_PORT = 2020
const DEFAULT_RECEIVE_ADDRESS = '0.0.0.0'
const DEFAULT_SEND_ADDRESS = '255.255.255.255'
const argv = yargs.scriptName('sudpee').help()
.command('receive', 'receive data over UDP', yargs => {
yargs.options({
port: {
alias: 'p',
describe: 'on which port to listen',
default: DEFAULT_PORT
},
address: {
alias: 'a',
describe: 'on which address to listen',
default: DEFAULT_RECEIVE_ADDRESS
},
timestamp: {
alias: 't',
describe: 'print timestamp on data receive'
},
sender: {
alias: 's',
describe: 'print address of sender'
}
})
.boolean(['date'])
}, receive).command('send', 'send data over UDP', function (yargs) {
return yargs.options({
message: {
alias: 'm',
describe: 'the message to be sent'
},
stdin: {
alias: 'i',
describe: 'send multiple messages from stdin'
},
port: {
alias: 'p',
describe: 'to which port to send',
default: DEFAULT_PORT
},
address: {
alias: 'a',
describe: 'to which address to send',
default: DEFAULT_SEND_ADDRESS
}
}).boolean(['stdin'])
}, send)
.argv
function receive (argv) {
sudpee.receive((msg, info) => {
const preStrings = []
if (argv.date) preStrings.push(new Date().toISOString())
if (argv.sender) preStrings.push(`${info.senderAddress}:${info.senderPort}`)
console.log(`${preStrings.join(' | ') + (preStrings.length > 0 ? ' | ' : '')}`, msg)
}, argv.port, argv.address)
.then(receiver => {
receiver.on('error', err => console.error(err))
console.log(`listening on: ${receiver.address}:${receiver.port}`)
})
.catch(err => console.error(err))
}
function send (argv) {
if (argv.message) {
sudpee.send(argv.message, argv.port, argv.address)
} else if (argv.stdin) {
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
prompt: 'message: '
})
rl.prompt()
rl.on('line', msg => {
msg.trim()
sudpee.send(msg, argv.port, argv.address)
rl.prompt()
})
} else {
console.error('No message or input provided')
}
}