becvert/cordova-plugin-websocket-server

wsserver.getInterfaces(function(result){...} giving no results

Dipayon3D opened this issue · 12 comments

The server successfully starts but the wsserver.getInterfaces(function(result) { . . . } does nothing . . . What should I do??

debug?
This is open source code. have a look into it.
sorry mate, without samples of your code or no error messages I can't do much for you!

Here's the complete code, both server side and client side . . . Please take a look . . .

var SERVICE_TYPE = '_my-service._tcp.',
HOST_NAME = 'my host';

function startServer(){

if(!wsserver){
alert("No WebSocket !");
return;
}else if(!zeroconf){
alert("No Zeroconf !");
return;
}

wsserver.start(0, {

  'onFailure' :  function(addr, port, reason) {
      navigator.notification.alert(
          'Stopped listening on '+addr+' : '+port+'. Reason: '+reason,  // message
          function(){},         // callback
          'Stopped',            // title
          'GOT IT . . .'                  // buttonName
      );
  },
'onOpen' : function(conn) {

    navigator.notification.alert(
        'A user connected from '+conn.remoteAddr,  // message
        function(){},         // callback
        'Connected',            // title
        'GOT IT . . .'                  // buttonName
    );
},

'onMessage' : function(conn, msg) {

    navigator.notification.alert(
        conn+" , "+msg,  // message
        function(){},         // callback
        'Message',            // title
        'GOT IT . . .'                  // buttonName
    );

},

'onClose' : function(conn, code, reason, wasClean) {

    navigator.notification.alert(
        "A user disconnected from "+conn.remoteAddr+", with Code - "+code+", because of "+reason+", Clean Connection : "+wasClean,  // message
        function(){},         // callback
        'Connected',            // title
        'GOT IT . . .'                  // buttonName
    );

},
// Other options
'origins' : [ 'file://'], // validates the 'Origin' HTTP Header. // [ 'file://',"http://","*" ]
'protocols' : [ 'my-protocol-v1', 'my-protocol-v2' ], // validates the 'Sec-WebSocket-Protocol' HTTP Header.
'tcpNoDelay' : true // disables Nagle's algorithm.

},

function onStart(addr, port) {

  // navigator.notification.alert(
  //     "Listening to "+addr+" at Port "+port,  // message
  //     function(){},         // callback
  //     'Server Started',            // title
  //     'GOT IT . . .'                  // buttonName
  // );

  var _interface = 'wlan0', ip_addresses;

  // Check whether we are listening on IPv4 or IPv6

  if (/^[0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}[.][0-9]{1,3}$/.test(addr)) {
      ip_addresses = 'ipv4Addresses';
  } else {
      ip_addresses = 'ipv6Addresses';
  }

  wsserver.getInterfaces(function(result) {

      alert("IP Adress "+ ip_addresses +", wsserver.get_interfaces() - Result "+ result +", ServerIP : "+ result[_interface][ip_addresses][0]);

      zeroconf.register(SERVICE_TYPE, 'local.', HOST_NAME, port, {

          server_ip: result[_interface][ip_addresses][0]

      }, function(data){

          navigator.notification.alert(
              data,  // message
              function(){},         // callback
              'Service successfully advertised',            // title
              'GOT IT . . .'                  // buttonName
          );


      }, function(cause){

         alert("Failed to advertise Service "+cause);

      });
      
  });

},

function onDidNotStart(reason) {
navigator.notification.alert(
reason, // message
function(){}, // callback
'Reason for not starting', // title
'GOT IT . . .' // buttonName
);
});

}

// Client Side

var connection;

function watchForServer(){

if(!zeroconf){
alert("No Zeroconf !");
return;
}

window.plugins.spinnerDialog.show("Looking For Service", "Please wait...", function(){});

zeroconf.watch(SERVICE_TYPE, 'local.', function(result){

  window.plugins.spinnerDialog.hide();

  var service = result.service, hostname, url;

  navigator.notification.alert(
      result,  // message
      function(){},         // callback
      'Results',            // title
      'GOT IT . . .'                  // buttonName
  );

  if (result.action === 'added') {

          hostname = service.txtRecord.server_ip;

      url = ['ws://', hostname, ':', service.port, '/'].join('');
      connection = new WebSocket(url);
      connection.onopen = function() {
          // Now the client is connected
          alert("Now the client is connected "+ result.service);
      }
  } else if (result.action == 'resolved') {
      alert("service resolved "+ result.service);
  }else{
      alert("Service Removed "+ result.service);
  }

}, function(cause){
// failed to watch for service
window.plugins.spinnerDialog.hide();
alert("Failed to watch "+ cause);
});

}

what's wrong !?

@becvert Do you see anything wrong in the code above ?? if so, please point it out to me . . .

wsserver.getInterfaces(function(result) { console.log(result); }, function(error) { console.log(error); })

what's the result of that?

No result ! No error or success ! I've waited for long time but nothing seems to be happening !

you don't have to wait... it just returns the interfaces.
are you testing on a real device or on a simulator?
what version of Android?

wsserver.getInterfaces(function(result) { console.log(result); }, function(error) { console.log(error); })
did you call it outside the onStart handler?

I am testing it on a real device, running Android 4.4.2 . . . And I'm calling "wsserver.getInterfaces( . . . )" INSIDE the "onStart" handler .

ok. you are sure that the onStart handler is called I suppose?

I would check that the android permissions are set in the manifest (android.permission.INTERNET, android.permission.ACCESS_WIFI_STATE)

I can't spend much more time with you on this. So I'll let you figure it out and debug the situation.
Please let us know once you fixed it. Thank you.

Yes, the onStart handler is being called . . . and the permissions are there . . . I'll keep trying anyway. I understand if you don't have time to look into it deeply, but if you do find something please let me know . . . Thank you.

@becvert I'm using CocoonJS . . . Does that have anything to do with it ?

@becvert getInterfaces( . . . ) is working ! The problem was that I was consoling out result.wlan0.ipv4Addresses[0] , but the result does not have a 'wlan0' in there. This is what it returns {"p2p0":{"ipv4Addresses":[],"ipv6Addresses":["fe80::bcd1:d3ff:fe51:ea60%p2p0"]}}

Is this normal ?? ipv4Addresses is empty ! What should I do next ??

p2p is likely a peer to peer connection (wifi direct maybe).
you're missing a normal wifi interface?
Please check you're networking settings and devices.
Closing for now.