Sunday, February 10, 2013

Roku SSDP nodejs

I tried using the SSDP to find the roku ip address using ncat as recommended by Roku, but using wireshark to moitor it just didn't seem to be working. In Ubuntu using sudo wireshark brings up the app, selecting your network interface under filter type in udp.dstport == 1900 and click apply, I was seeing my router and the roku box notifying the 239.255.255.250 ip of their ip addresses, but I wasn't seeing the "M-SEARCH * HTTP/1.1 showing up. I wanted a nodejs solution so I tried searching for that, hoping that might change things up, I found this:

var dgram = require('dgram'); // dgram is UDP
// Listen for responses
function listen(port) {
var server = dgram.createSocket("udp4");
server.on("message", function (msg, rinfo) {
console.log("server got: " + msg + " from " + rinfo.address + ":" + rinfo.port);
});
server.bind(port); // Bind to the random port we were given when sending the message, not 1900
// Give it a while for responses to come in
setTimeout(function(){
console.log("Finished waiting");
server.close();
},2000);
}
function search() {
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:1\r\n" + // 1 second to respond (but they all respond immediately?)
"\r\n"
);
var client = dgram.createSocket("udp4");
client.bind(); // So that we get a port so we can listen before sending
listen(client.address().port);
client.send(message, 0, message.length, 1900, "239.255.255.250");
client.close();
}
search();
view raw ssdp.node.js hosted with ❤ by GitHub
However placing the roku parameters ST: just wasn't working"roku:ecp", I did notice in the notify from the box that it was using MX as 2, so I tried updating that, however still no luck. After consulting nodejs.org I discovered the optional call back and in their example they close the connection when the call is completed. So I made the update to the following.

It appears that the program was closing the client connection before the message was sent out (and a response occurs), so once I put in the callback to close it, I got a response. Hope that helps anyone else who wants to use SSDP.

 Check out the Roku Remote nodejs module I am working on.