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

BSD 3-Clause "New" or "Revised" License
79 stars 40 forks source link

receive udp packet data field is null #17

Open shansjlin opened 7 years ago

shansjlin commented 7 years ago

My code is shown as below, I create udp socket, set broadcast, bind receiveListener,and then send my request packet ipRequest to my udpServer:

receiveListener(info) {
let self = this;
console.log('wifiStore Recv from socket: ' + JSON.stringify(info));
}

startSearch() {
let self = this;
self.resetWifi();
if(window.chrome) {
  let ipRequest = [{msgType: 'ipRequest'}];
  let ipRequestStr = JSON.stringify(ipRequest);
  chrome.sockets.udp.onReceiveError.addListener(self.receiveErrorListener.bind(this));
  chrome.sockets.udp.onReceive.addListener(self.receiveListener.bind(self));
  chrome.sockets.udp.create(function(createInfo) {
    self._socketId = createInfo.socketId;
    console.log('startSearch createInfo.socketId:' + self._socketId);
    chrome.sockets.udp.bind(createInfo.socketId, '0.0.0.0', 0, function(result) {
      console.log('startSearch bind result:' + result);
      chrome.sockets.udp.setBroadcast(createInfo.socketId, true, function(result) { 
        console.log('startSearch setBroadcast result:' + result);

        self._intervalId = setInterval(function() {
          let message = self.stringToArrayBuffer(ipRequestStr);
          chrome.sockets.udp.send(createInfo.socketId, message, '255.255.255.255', 8081, function(result) {
            if (result < 0) {
              console.log('startSearch fail: ' + result);
            } else {
              console.log('startSearch success ' + result);
            }
          });
        } ,1000);

      });
    });
  });
} else {
  console.log('startSearch not window.chrome');
}

} And my udpServer receive the request, as the log shown below: serverLog - recv [{"msgType":"ipRequest"}](25 bytes) from client 192.168.1.112:63912 serverLog - recv [{"msgType":"ipRequest"}](25 bytes) from client 192.168.1.112:63912 serverLog - recv [{"msgType":"ipRequest"}](25 bytes) from client 192.168.1.112:63912

And the udpServer send the answer packet to my request client, the answer code is shown as below:

 var serverSocket = dgram.createSocket('udp4');
 serverSocket.on('message', function(msg, rinfo){
logger.warn('recv %s(%d bytes) from client %s:%d', msg, msg.length, rinfo.address, rinfo.port);
var msgStr = String(msg);
var message = JSON.parse(msgStr);
for (var i = 0; i < message.length; i++) {
  if (message[i].msgType && message[i].msgType === 'ipRequest') {
    if (ackFlag){
      var ip = getIPAdress();
      var ipAck = [{msgType: 'ipAck', IP: ip}];
      var msg = JSON.stringify(ipAck); 
      logger.debug('ack to client');
      serverSocket.send(msg, 0, msg.length, rinfo.port, rinfo.address);
    }
  }
}    

});

And my request client receive the answer packet, but the data is null, as shown below: Recv from socket: {"socketId":0,"data":{},"remoteAddress":"192.168.1.125","remotePort":8081} Recv from socket: {"socketId":0,"data":{},"remoteAddress":"192.168.1.125","remotePort":8081}

the data field is null, but my expected data is "var ipAck = [{msgType: 'ipAck', IP: ip}];"

Anyone who can show me why? Thanks very much!!!

By the way, the request client is a cordova app, and is tested on IPad

charliemciver commented 7 years ago

Hopefully your UDP Server send function is working..... maybe used a UDP network monitor like PAcketSender to check....

In your receive event handler, I think your missed a step to get the data out of the info object and then reconvert it from a byte array to a string for the JSON handler.

receiveListener(info) { let self = this; data_string = self.arrayBuffertoString(info.data) //to be written - many examples out there. console.log('wifiStore Recv from socket: ' + data_string); }

shansjlin commented 7 years ago

@charliemciver Thanks very much. It works as you said. Info.data need to be reconverted from arrayBuffer to string. Thanks, you are so great.