Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to do UDP broadcast using chrome.sockets.udp API?

I am developing a Chrome extension and I want to broadcast a UDP packet on the local network.

I studied this Chrome API.

 chrome.sockets.udp.create({}, function(s){   
        chrome.sockets.udp.bind(s.socketId, address, 0, function(ret){
            chrome.sockets.udp.send(s.socketId, data, "172.16.0.0", 5019, 
                function(sendinfo){console.log(data.byteLength); console.log(sendinfo);})})})

If I specified a address like 172.16.0.0, the above code is OK. But If I changed 172.16.0.0 to 255.255.255.255, I got {resultCode: -10} which indicates an error.

My manifest.json:

{
  "manifest_version": 2,
  "name": "UDP",
  "description": "Test",
  "version": "2",
  "minimum_chrome_version": "23",
  "app": {
    "background": {
      "scripts": ["main.js"]
    }
  },
  "sockets":{
    "udp": {"send":["*:*"], "bind":["*:*"]}
  }, 
  "permissions":["system.network"]
}

By the way, I tried chrome.socket which works fine even on broadcast. But the API is deprecated starting with Chrome 33.

like image 251
onemouth Avatar asked Jun 09 '14 10:06

onemouth


People also ask

Can sockets be UDP?

UDP socket routines enable simple IP communication using the user datagram protocol (UDP). The User Datagram Protocol (UDP) runs on top of the Internet Protocol (IP) and was developed for applications that do not require reliability, acknowledgment, or flow control features at the transport layer.

Does Chrome support UDP?

Chrome Apps can act as a network client for TCP and UDP connections. This doc shows you how to use TCP and UDP to send and receive data over the network. For more information, see the Sockets UDP, Sockets TCP and Sockets TCP Server APIs.


2 Answers

Since Chrome 44, we have setBroadcast API.

https://developer.chrome.com/apps/sockets_udp#method-setBroadcast

like image 136
onemouth Avatar answered Oct 01 '22 23:10

onemouth


Haven't tried it yet, but I found this:

"sockets": {
  "udp": {
    "bind": "*",
    "send": "*",
    "multicastMembership": ""
  }
}

In this bug report

The empty string value for "multicastMembership" isn't obvious at all, I had to resort to reading the C++ unit tests to find out the correct value.

like image 44
Yuval Aviguy Avatar answered Oct 01 '22 23:10

Yuval Aviguy