• Here is the code (which won't run in isolation) as it requires files too...

    var model = {
      setpoint : 66,
      timer : 60,
      temp: [50,50]
      //  counter : 3600
    };
    
    var event_cb = {
      timer : function (t) {
        model.counter = t * 60;
      }
    };
    
    event_cb.timer(model.timer);
    
    function tick() {
      var d = new Date(model.counter * 1000);
      model.counterstr = d.toString().split(' ')[4];
      model.counter -= 1;
      model.free=process.memory().free;
      model.usage=process.memory().usage;
    
    }
    
    setInterval(tick, 1000);
    
    
    
    function onPageRequest(req, res) {
    
      var a = url.parse(req.url, true);
      /**/
     // console.log(a);
    
      console.log({sckt:req.sckt});
      console.log(req.headers);
      //console.log(req.method);
       /**/
      var file = a.pathname;
      if (file == '/') {
        file = '/index.htm';
      }
      var headers = {};
      if (req.method == 'PUT')
        return put_file(req, res);
      var f;
      try {
        f = E.openFile('www' + file, "r");
      } catch (e) {
        // look for compressed
        try {
          f = E.openFile('www' + file + '.gz', "r");
          headers['Content-Encoding'] = 'gzip';
        } catch (e) {
          console.log('no file' + file);
        }
      }
    
      if (f !== undefined) {
        var mime = 'text/html';
        if (file.substr(-2) == 'js')
          mime = 'application/javascript';
        if (file.substr(-3) == 'css')
          mime = 'text/css';
        if (file.substr(-3) == 'ico')
          mime = 'image/vnd.microsoft.icon';
    
        headers['Content-Type'] = mime;
    
        res.writeHead(200, headers);
        console.log('started:' + file );
        f.pipe(res, {
          chunkSize : 512,
          end : false,
          complete : function () {
            console.log("Complete:" + file);
            f.close();
            res.end();
          }
        });
    
      } else {
        res.writeHead(404, {
          'Content-Type' : 'text/plain'
        });
        res.end("404: Page " + a.pathname + " not found");
      }
    }
    
    var server = require('ws').createServer(onPageRequest­);
    
    var update_model=false;
    
    server.on("websocket", function (ws) {
      ws.on('message', function (msg) { //console.log((msg));
        var data = JSON.parse(msg);
        console.log(data);
        if ( update_model ) {
        for (var o in data) {
          //console.log( o, data[o] );
          model[o] = data[o];
          if (event_cb.hasOwnProperty(o)) {
            event_cb[o](data[o]);
          }
        }
        }
      });
      function send_model() {
        var s=JSON.stringify(model);
        ws.send(s);
        s=null;
      }
      setInterval(send_model, 1000);
    });
    
    function start() {
      server.listen(8080);
    }
    
    start();
    
About

Avatar for Wilberforce @Wilberforce started