Last active
March 23, 2016 01:03
ssdp discovery with node.js
This file contains hidden or 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
// 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