running node.js with forever to avoid crashing server

tillfly's icon

Hey,
i´m running a node.script which connects to a tcp server automatically after starting the max patch.
somehow the connection crashes after one to two hours. ( i can not be personally thats why i dont know exactly )
the patch runs as a standalone. the computer on which it runs was licensed in development but doesnt have a license right now, which should make no difference I think.

now I would like to run this code with forever to restart the node script immediatelly after crashing or figure out some other logic to restart after crashing...

how could I do this?

this is the node script:

// connection to max
const path = require('path');
const maxApi = require('max-api');

const net = require('net');

var socketClient;
var connected = false;
var chunk = "";
var delimiter = ';';

reconnect();

function reconnect()
{
    reconnectTimer = setInterval(function() 
    {
    /* Instance socket on create window */
    console.log('Try to connect');
    // maxApi.outlet('Try to connect');

    socketClient = net.connect({
       host: 'x.x.x.x',
       port: 8080
    }, () => {
       // 'connect' listener
       console.log('connected to server!');
        clearInterval(reconnectTimer);
       //maxApi.outlet('connected to server');
       //socketClient.write('world!\r\n');
       
       socketClient.write("bla1,1:bla2,1;");

    });

    socketClient.on('error', function (ex) 
    {
       console.log("sensor server error");
       console.log(ex);
    });

    // how to send data to max, test wich SocketTest 3.0v

    socketClient.on('data', (data) => {
    chunk += data.toString(); // Add string on the end of the variable 'chunk'
    //console.log(data.toString());
    d_index = chunk.indexOf(delimiter); // Find the delimiter

    // While loop to keep going until no delimiter can be found
    while (d_index > -1) {         
        try {
            string = chunk.substring(0,d_index); // Create string up until the delimiter
            console.log(string);
            //case AIN-0,value;

            //console.log(string);
            process(string); // Function that does something with the current chunk of valid json.        
        }
        catch(e)
        {
          console.log(e);
        }
        chunk = chunk.substring(d_index+1); // Cuts off the processed chunk
        d_index = chunk.indexOf(delimiter); // Find the new delimiter
    }      
        //console.log('Hello ' + person.prenom + "!");

    });

    });
}

  function process(string) {

               //console.log(obj.sensor_data.value);
               // value: 0 muss weiter an max
               // console.log(data.sensor_data.value);

                // to max msp
                string = string.substring(string.indexOf(',')+1);
                var data = parseInt(string, 10);
               maxApi.outlet(data);
           
    // body...
  }

   socketClient.on('end', () => {
       console.log('disconnected from server');
       // CRASH FLAG HERE TO RESTART via "script start" to node.script object in Max?
   }); 

tillfly's icon

scope problem ...