Created
October 1, 2018 23:28
-
-
Save PierBover/34ab4222a49bfd121b6ab21d60572de6 to your computer and use it in GitHub Desktop.
Node.js UDP Broadcast example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const dgram = require('dgram'); | |
const message = new Buffer('Server?'); | |
const socket = dgram.createSocket('udp4'); | |
socket.on('listening', function () { | |
socket.setBroadcast(true); | |
setInterval(() => { | |
socket.send(message, 0, message.length, 5555, '255.255.255.255'); | |
}, 5000); | |
}); | |
socket.on('message', function (message, remote) { | |
console.log('CLIENT RECEIVED: ', remote.address + ':' + remote.port +' - ' + message); | |
}); | |
socket.bind('8888'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const dgram = require('dgram'); | |
const socket = dgram.createSocket('udp4'); | |
socket.on('listening', function () { | |
const address = socket.address(); | |
console.log('UDP socket listening on ' + address.address + ":" + address.port); | |
}); | |
socket.on('message', function (message, remote) { | |
console.log('SERVER RECEIVED:', remote.address + ':' + remote.port +' - ' + message); | |
const response = "Hellow there!"; | |
socket.send(response, 0, response.length, remote.port, remote.address); | |
}); | |
socket.bind('5555'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment