Skip to content

Instantly share code, notes, and snippets.

@aprock
Last active March 23, 2016 01:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aprock/c535be4ea82e64d2367d to your computer and use it in GitHub Desktop.
Save aprock/c535be4ea82e64d2367d to your computer and use it in GitHub Desktop.
ssdp discovery with node.js
// original: https://gist.github.com/chrishulbert/895382
var dgram = require('dgram'); // dgram is UDP
var matcher = process.argv[2] ? new RegExp(process.argv[2]) : undefined;
function search() {
var client = dgram.createSocket("udp4");
client.bind();
client.once('listening', function() {
var message = new Buffer(
"M-SEARCH * HTTP/1.1\r\n" +
"HOST:239.255.255.250:1900\r\n" +
"MAN:\"ssdp:discover\"\r\n" +
"ST:ssdp:all\r\n" + // Essential, used by the client to specify what they want to discover, eg 'ST:ge:fridge'
"MX:2\r\n" + // 2 seconds to respond (but they all respond immediately?)
"\r\n"
);
client.send(message, 0, message.length, 1900, "239.255.255.250", function(err) {
if (err) throw err;
console.log('message was sent');
});
});
client.on('message', function(msg, rinfo) {
if (!matcher || matcher.test(msg)) {
console.log("client got: " + msg + " from " + rinfo.address + ":" + rinfo.port);
}
});
}
search();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment