Skip to content

Instantly share code, notes, and snippets.

@qgustavor
Forked from ciaranj/gist:9056285
Last active August 11, 2016 11:54
Show Gist options
  • Save qgustavor/021d5e77347da9e8bd1b88ea9a51b2fd to your computer and use it in GitHub Desktop.
Save qgustavor/021d5e77347da9e8bd1b88ea9a51b2fd to your computer and use it in GitHub Desktop.
A *working* (on Windows) UDP Multicast client & server with Node.Js v0.10.25 (I spent a *LOT* of time getting EINVAL and I still don't quite know why :/)
var PORT = 5007 ;
var dgram = require('dgram');
var client = dgram.createSocket('udp4');
client.on('listening', function () {
var address = client.address();
console.log('UDP Client listening on ' + address.address + ":" + address.port);
client.setBroadcast(true)
client.setMulticastTTL(128);
client.addMembership('224.1.1.1');
});
client.on('message', function (message, remote) {
console.log('A: Epic Command Received. Preparing Relay.');
console.log('B: From: ' + remote.address + ':' + remote.port +' - ' + message);
});
client.bind(PORT);
var news = [
"Borussia Dortmund wins German championship",
"Tornado warning for the Bay Area",
"More rain for the weekend",
"Android tablets take over the world",
"iPad2 sold out",
"Nation's rappers down to last two samples"
];
var dgram = require('dgram');
var server = dgram.createSocket("udp4");
server.bind( function() {
server.setBroadcast(true)
server.setMulticastTTL(128);
setInterval(broadcastNew, 3000);
});
function broadcastNew() {
var message = new Buffer(news[Math.floor(Math.random()*news.length)]);
server.send(message, 0, message.length, 5007, "224.1.1.1");
console.log("Sent " + message + " to the wire...");
}
@qgustavor
Copy link
Author

By the way... why those weird messages?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment