MobileChromeApps / cordova-plugin-chrome-apps-sockets-udp

BSD 3-Clause "New" or "Revised" License
80 stars 39 forks source link

Cannot send UDP packet #27

Open classictoni opened 5 years ago

classictoni commented 5 years ago

Hi, I am trying to send a UDP packet from my Android device. My code looks as follows:

chrome.sockets.udp.create({}, function(createInfo) {
    const socketId = createInfo.socketId;
    const arrayBuffer = new ArrayBuffer(2);
    arrayBuffer[0] = 0x01;
    arrayBuffer[1] = 0xff;
    console.log('socketId: ' + createInfo.socketId);
    chrome.sockets.udp.send(socketId, arrayBuffer,
      '127.0.0.1', 1337, function(sendInfo) { 
        console.log('sent ' + sendInfo.bytesSent); // this log never happens
      });
});

Why isn't it sending anything?

I am using

classictoni commented 5 years ago

I found a solution that works, when binding the socket before sending data (from receiving data example):

var socketId;

// Handle the "onReceive" event.
var onReceive = function(info) {
  if (info.socketId !== socketId)
    return;
  console.log(info.data);
};

// Create the Socket
chrome.sockets.udp.create({}, function(socketInfo) {
  socketId = socketInfo.socketId;
  // Setup event handler and bind socket.
  chrome.sockets.udp.onReceive.addListener(onReceive);
  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, arrayBuffer,
        '127.0.0.1', 1337, function(sendInfo) {
          console.log("sent " + sendInfo.bytesSent);
      });
  });
});
ebrahemimorteza commented 2 years ago

For me this code does not work I checked everything the plugin is installed but it does not work

ebrahemimorteza commented 2 years ago

If you have a solution, tell me, thank you in advance