Error connecting to the Socket server

0

There is some way to catch if the socket server I'm trying to connect to is (off, not available etc.).

This is my code:

 import http from 'http';
 import socket_io from 'socket.io';

In the event of my button

  try{

  var net = require('net');
  var client = net.connect(12346, 'localhost');
  client.setEncoding('utf8');

  client.on('data', function(data) {
    console.log("Recibiendo :", arguments)
  });

  client.on('disconnect', function() {
    console.log("Error al conectarme con el servido");
  });

  client.on('end', () => {
    console.log('desconectado del servidor');
  });

  client.on('close', function() {
      console.log('Conexion cerrada');
    });

}catch(error) {
  console.log("Error al conectarme con el servidor :"+error);
}

I have no problems when I run the server, but if the socket server is stopped or I have not started it, it does not show any of my logs and the app corrupts showing me the message:

  

connect ECONNREFUSED 127.0.0.1:12346 and

     

TCPConnectWrap.afterConnect [as oncomplete]

    
asked by Rastalovely 21.09.2017 в 21:02
source

1 answer

1
  

What I need is to show a message that says error when connecting to the server.

That you must do with the event error :

client.on('error', (err) => {
  console.log('Error al conectarse al servidor', err.message);
});
    
answered by 22.09.2017 / 16:52
source