You are reading a single comment by @SteveHayles and its replies. Click here to read the full conversation.
  • Thanks @allObjects. I have rewritten the socket code many times to try and make this work but the result is always similar. I have refactored things now to be more inline with the guide (although the API reference shows a similar pattern to the one above). Here is the exact code of my running application

    function connectNmea() {
      var client = require("net").connect(NMEA_OPTIONS, function(socket, err) {
        if (err) {
          console.log("Connection error: "+err);
          setTimeout(connectNmea, 5000);
          return;
        }
        client.on('end', function() {
          console.log("Nmea socket closed");
          EventBus.clear('gpsData');
          setTimeout(connectNmea, 5000);
        });
        EventBus.subscribe('gpsData', function(data) {
          client.write(data);
        });
      });
    }
    

    The EventBus is a simple implementation that queues messages in a simple pub / sub pattern. The EventBus.subscribe('gpsData',... code is simply pulling data out of a queue from the Serial Port. The server is a separate application that I cannot alter but I have stress tested it and it will handle hundreds of concurrent connections without difficulty.

    Debugging the state of the client when the 'end' event is called it looks like

    =Socket: { "type": 0,
      "#onconnect": function (socket,err) { ... },
      "opt": {
        "host": "192.168.1.12",
        "port": 10110 },
      "conn": false,
      "#onend": function (data) { ... },
      "dSnd": "",
      "cls": true, "endd": true }
    

    and then 5 seconds later in the reconnection it appears to be properly initialized

    =Socket: { "type": 0,
      "#onconnect": function (socket,err) { ... },
      "opt": {
        "host": "192.168.1.12",
        "port": 10110 },
      "sckt": 2, "conn": true }
    

    but will immediately be closed again and won't sent data. Ignoring the 'end' and leaving the code still running will report (obviously!)"function called from system Uncaught Error: This socket is closed"

    I agree that blind socket reconnects don't always make sense but the link is still up the server is still running and the code doesn't call for a disconnect. Happy to do any amount of debugging but very keen to make this work. Thanks

About

Avatar for SteveHayles @SteveHayles started