-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathudp.js
More file actions
36 lines (33 loc) · 1.02 KB
/
udp.js
File metadata and controls
36 lines (33 loc) · 1.02 KB
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
const dgram = require('dgram')
const _ = require('lodash/fp.js')
const { hexToBuff } = require('nori-can')
function getDataBuffer({ data, format }) {
if (format === 'hex') return Buffer.from(hexToBuff(data))
if (_.isString(data)) return Buffer.from(data)
// Default assumes can be consumed by Buffer.from().
return Buffer.from(data)
}
const sendUdp = _.curry((udpSocket, info) => {
const { port, address } = info
const data = getDataBuffer(info)
udpSocket.send(data, 0, data.length, port, address, (err) => {
if (err) {
console.log('UDP ERR', err)
} else {
console.log('UDP TX', address, port, data.length)
}
})
})
function createSendUdp(portFrom = null, bcast = false, portTo = null) {
const udpSocket = dgram.createSocket('udp4')
if (portFrom) {
console.log('createSendUdp', portFrom)
udpSocket.bind({ port: portFrom }, () => {
if (bcast) udpSocket.setBroadcast(true)
})
}
return portTo ? sendUdp(udpSocket, portTo) : sendUdp(udpSocket)
}
module.exports = {
createSendUdp,
}