You are reading a single comment by @SteveHayles and its replies. Click here to read the full conversation.
  • Hi,

    Using an Espruino Wifi with the 'EspruinoWifi' module running Firmware 1.99.

    The question may expand into other issues but directly my question is around an un-recoverable socket disconnection. I am using the 'Net' module and connecting to a simple Tcp Server. All works well until the socket appears to be closed (either from native code or the module itself but NOT from my code)

    This might be a transient socket disconnection for whatever reason but the main problem is that I can't reconnect. Subscribing to the socket.on('close') event works and I have tried a simple reconnect via a timeout function. This function runs and the callback passes an empty 'err' object and a complete socket as it does first time round but no data is received and the 'close' event is triggered again instantly.

    My code looks like this

    function connect() {
      console.log("Attempting connection");
      client.connect(MY_OPTIONS, function(socket, err) {
        if (err) {
          console.log("Its all gone bad and here's why: "+err);
          return;
        }
        console.log("Connected to My Server ");
    
        //my socket.on('data', .... function lives here
    
        socket.on('close', function() {
          console.log("socket closed");
          setTimeout(function() {
            connect();
          }, 5000);
        });
      });
    }
    

    In summary, it works great until the socket closed event gets called. The 2nd connect runs with no error and return a socket object but no data is received and the close event fires immediately.

    Any thoughts on how to recover from the socket disconnection or maybe I can do something to prevent it. Thanks

About

Avatar for SteveHayles @SteveHayles started