Ionic 2 App and server on localhost communication not working

543 Views Asked by At

I´m building a Ionic 2 App for windows 10 that needs to communicate with a server running on localhost.

All works good if I run my application in a browser (either with ionic serve or ionic run browser) but the communication between App and LocalServer is not working if I launch the application with ionic run windows and I´m unable to get any error message (the rest of the App works fine).

In a scenario where the server runs on another device (not using localhost) all works good again... The issue is that I need to have the App and the server running on the same device, any tip on what is hapening and how this can be solved?

Let me know if something is not clear enough. Thanks in advance!

Server:

var io = require('socket.io')();
io.on('connection', function(client){
  console.log('Connected...');
  client.on('message', function(data){
    console.log('message: ' + data);
  });
  client.on('disconnect', function() { console.log('Disconnected...'); });
});
io.listen(3300);

Client methods (Ionic 2 App):

  connect() {
    console.log('Connect...');
    this.socket = io.connect('http://127.0.0.1:3300');
  }
  disconnect() {
    console.log('Disconnect...');
    this.socket.disconnect();
  }
  send() {
    console.log('Send...');
    this.socket.emit('message', 'test message...');
  }

Edit

Another strange behavior that might help describing the problem:

if I launch the server and the application and call the connect method (from the client) nothing happens in the server... but just by restarting the server I´m able to see the result of the console.log('Connected...');. Since the client is configured to automatically reconnect after a connection is lost, the console.log('Connected...'); is probably executed due to this re-connection request.

This means that, in some way, the Ionic App client is able to reach the server, although I´m not able to figure out why this just happens after restarting the server. Even after that I´m not able to have real time communication, the problem is the same, I´m only able to get some activity in the server upon restarting it.

0

There are 0 best solutions below