Skip to content

Instantly share code, notes, and snippets.

@wankdanker
Created November 2, 2011 18:51
Show Gist options
  • Save wankdanker/1334509 to your computer and use it in GitHub Desktop.
Save wankdanker/1334509 to your computer and use it in GitHub Desktop.
simple test cases for broadcast and multicast compatibility between node.js v0.4.x and v0.6.x
var dgram = require('dgram');
var socket = dgram.createSocket('udp4');
var testMessage = "[hello world] pid: " + process.pid;
var broadcastAddress = '255.255.255.255';
var broadcastPort = 5555;
socket.setBroadcast(true);
socket.bind(broadcastPort, '0.0.0.0');
socket.on("message", function ( data, rinfo ) {
console.log("Message received from ", rinfo.address, " : ", data.toString());
});
setInterval(function () {
socket.send(new Buffer(testMessage),
0,
testMessage.length,
broadcastPort,
broadcastAddress,
function (err) {
if (err) console.log(err);
console.log("Message sent");
}
);
}, 1000);
var dgram = require('dgram');
var socket = dgram.createSocket('udp4');
var testMessage = "[hello world] pid: " + process.pid;
var multicastAddress = '239.1.2.3'
var multicastPort = 5554
socket.addMembership(multicastAddress);
socket.bind(multicastPort, '0.0.0.0');
socket.on("message", function ( data, rinfo ) {
console.log("Message received from ", rinfo.address, " : ", data.toString());
});
setInterval(function () {
socket.send(new Buffer(testMessage),
0,
testMessage.length,
multicastPort,
multicastAddress,
function (err) {
if (err) console.log(err);
console.log("Message sent");
}
);
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment