Avatar for Stephen

Stephen

Member since Apr 2017 • Last active May 2017
  • 1 conversations
  • 1 comments

Most recent activity

  • in Pico / Wifi / Original Espruino
    Avatar for Stephen

    I have been doing some testing on net.connect and find it runs the callback before the connection is established (deliberately erroneous hostname and port in code). I can't then run a registered error event handler. This is counter to the documents that indicate that the callback is executed after the TCP connection is established. I would appreciate some validation that I am not doing anything wrong?

    There is also no way I can see to change the connect timeout, however if I use the AT commands I can catch the error.

    Output:

    **** STARTING ****
    WiFi Connected!
    ["AT+CIPSTART=0,\"TTCP\",\"weeso\",1888\­r\n"
    net client before callback {"type":0,"opt":{"host":"weeso","port":1­888},"sckt":1}
    net client callback {"type":0,"opt":{"host":"weeso","port":1­888},"sckt":1,"conn":true}
    net client disconnected {"type":0,"opt":{"host":"weeso","port":1­888},"conn":true} *** {"type":0,"opt":{"host":"weeso","port":1­888},"conn":true}
    net client disconnected {"type":0,"opt":{"host":"weeso","port":1­888},"conn":true} *** undefined
    net closed, had_error: false
    

    Code:

    var wifi = require("EspruinoWiFi");
    
    function onInit() {
      console.log("**** STARTING ****");
      wifi.connect(WIFI_NAME, WIFI_OPTIONS, function (err) {
        if (err) {
          console.log("WiFi Connection error: " + err);
          return;
        }
        console.log("WiFi Connected!");
        wifi.at.debug();
        var at = wifi.at;
        //at.cmd('AT+CIPSTART=0,"TCP","weeso",18­88\r\n', 10000, (cb) => {
        //  console.log('AT cb '+cb);
        //});
    
        var client = require("net").connect({host: "weeso", port: 1888}, function (skt) {
          console.log('net client callback ' + JSON.stringify(skt));
          //client.write('Hello');
          client.on('error', function (err) {
            console.log("net error ");
            console.log("Error: " + JSON.stringify(err));
          });
          client.on('data', function (data) {
            console.log("net > " + JSON.stringify(data));
          });
          client.on('connected', function(data) {
            console.log("net connect "+JSON.stringify(data));
          });
          client.on('timeout', function (data) {
            console.log("net timeout " + JSON.stringify(data));
          });
          client.on('close', function (data) {
            console.log("net closed, had_error: " + JSON.stringify(data));
          });
          client.on('end', function (err) {
            console.log('net client disconnected ' + JSON.stringify(skt) + ' *** ' + JSON.stringify(err));
          });
        });
        console.log('net client before callback ' + JSON.stringify(client));
    
        });
    }
    
Actions