Trying to discover Plex Media Server on network using chrome.sockets.udp

380 views
Skip to first unread message

iSh0w M

unread,
Jul 11, 2015, 1:12:02 PM7/11/15
to chromi...@chromium.org
Hey,

Trying to get this working, able to bind and then send bytes but unable to receive data. Am on Chrome OS.
Need to:
1. Send a UDP message to the multicast address 239.0.0.250 on port 32414: M-SEARCH * HTTP/1.0

2. listen for responses from each PMS returned to port 32414 starting "200 OK"

The returned message body will contain some information about the server - the source IP of this message will be the PMS IP.




Not necessarily looking for plex specific solution, if someone could just help with receiving data that would be great! 


Excerpt from manifest.json
  "permissions": [ "storage" , "identity", "https://www.googleapis.com/", "clipboardRead", "system.network" ],
 
"sockets": {
   
"udp": {
     
"bind" : "*",
     
"send" : "*"
   
}



Excerpt from main.js 
var socketId;
   
// Handle the "onReceive" event.
   
var onReceive = function(info) {
     
var data = chrome.socket.read(info.socketId);
     
if (info.socketId !== socketId)
       
return;  
      console
.log("info.data received "+info.data);
      console
.log("info.data received "+data);
   
};
   
//on receive error
   
function onReceiveError(info) {
        console
.log("error with: ");
        console
.log(info);
   
}
   
// Create the Socket
    chrome
.sockets.udp.create({}, function(socketInfo) {
        socketId
= socketInfo.socketId;
      chrome
.sockets.udp.bind(socketId,
       
"0.0.0.0", 0, function(result) {
         
if (result < 0) {
            console
.log("Error binding socket.");
           
return;
         
}
          chrome
.sockets.udp.send(socketId, stringToArrayBuffer("HELLO"),
           
'239.0.0.250', 32414, function(sendInfo) {
             
//At this point get 10 bytes sent
              console
.log("sent " + sendInfo.bytesSent);
             
if (sendInfo.resultCode < 0) {
                console
.log("Error listening: " + chrome.runtime.lastError.message);
             
}
     
// Setup event handler and bind socket.
      chrome
.sockets.udp.onReceive.addListener(onReceive);
      chrome
.sockets.udp.onReceiveError.addListener(onReceiveError);
         
});
     
});


   
});
   
function stringToArrayBuffer(string) {
   
var buffer = new ArrayBuffer(string.length * 2);
   
var bufferView = new Uint16Array(buffer);
   
for (var i = 0, stringLength = string.length; i < stringLength; i++) {
        bufferView
= string.charCodeAt(i);
       
}
         
return buffer;
   
}

iSh0w

unread,
Jul 17, 2015, 12:04:06 AM7/17/15
to chromi...@chromium.org
Anyone?

John Judge

unread,
Jul 24, 2015, 4:06:10 AM7/24/15
to Chromium Apps, ish0w...@gmail.com
Can you see a packet being returned using tcpdump or similar? You may have a race condition in your code between adding the listener and sending the packet. You may find that you are missing returning packets as the listener is not in place in time.

iSh0w

unread,
Jul 25, 2015, 12:00:47 AM7/25/15
to Chromium Apps, john....@nicta.com.au
Thanks John, appreciate it! Someone else in another forum pointed out that I was calling the listener a little too late as well. Changed that, also added chrome.sockets.udp.joinGroup and added multicast to the manifest.json, no joy :/

/iSh0w


Excerpt from manifest.json
  "permissions": [ "storage" , "identity", "https://www.googleapis.com/", "clipboardRead", "system.network" ],
 
"sockets": {
   
"udp": {
     
"bind" : "*",

     
"send" : "*",
     
"multicastMembership": ""
   
}

excerpt from main.js
function stringToArrayBuffer(string) {
   
var buffer = new ArrayBuffer(string.length * 2);
   
var bufferView = new Uint16Array(buffer);
   
for (var i = 0, stringLength = string.length; i < stringLength; i++) {
        bufferView
= string.charCodeAt(i);
       
}
         
return buffer;
   
}

       
    chrome
.sockets.udp.create({bufferSize: 1024 * 1024}, function (createInfo) {

     
// Handle the "onReceive" event.

   
var onReceive = function onReceive(info) {

     
var data = chrome.socket.read(info.socketId);
     
if (info.socketId !== socketId)
       
return;  
      console
.log("info.data received "+info.data);
      console
.log("info.data received "+data);
   
};

    chrome
.sockets.udp.onReceive.addListener(onReceive);
    chrome
.sockets.udp.onReceiveError.addListener(onReceiveError);
   
var socketId = createInfo.socketId;

   
//on receive error
   
function onReceiveError(info) {

        console
.log("onReceiveError");
        console
.log("info " + info);
   
}
     
//console.log("me.config.port "+me.config.port);
      chrome
.sockets.udp.bind(socketId, "0.0.0.0", 0, function (result) {
       
if (result !== 0) {
          chrome
.sockets.udp.close(socketId, function () {
            console
.log("Error on bind(): ", result);
         
});
       
} else {
          chrome
.sockets.udp.joinGroup(socketId, "239.0.0.250", function (result) {
           
if (result !== 0) {
              chrome
.sockets.udp.close(socketId, function () {
                console
.log("Error on joinGroup(): ", result);
             
});
           
} else {
              console
.log("socketID " + socketId);
              chrome
.sockets.udp.send(socketId, stringToArrayBuffer("M-SEARCH * HTTP/1.0"),
             
'239.0.0.250', 32414, function(sendInfo) {
                  console
.log("sent " + sendInfo.bytesSent);
                  console
.log("resultcode " + sendInfo.resultCode);

                 
if (sendInfo.resultCode < 0) {
                      console
.log("Error listening: " + chrome.runtime.lastError.message);
                 
}

                 
else if(sendInfo.resultCode === 0){
                   
//No Plex Media Servers found
                    console
.log("resultcode is 0");
             
}});


           
}
         
});
       
}
     
});
 
});

John Judge

unread,
Jul 26, 2015, 10:43:45 PM7/26/15
to Chromium Apps, ish0w...@gmail.com
You do not need to call read inside the Listener function. The packet data is already passed to you as an ArrayBuffer in info.data. Pass the port used for the multicast packet to the call to bind. Again, use a program like tcpdump or wireshark to look at the packets actually being sent and received.
Message has been deleted

iSh0w

unread,
Jul 27, 2015, 7:39:50 AM7/27/15
to Chromium Apps, john....@nicta.com.au
Thank you, your instructions worked for me. Needs more testing/refinement but works.

/iSh0w
Reply all
Reply to author
Forward
0 new messages