• Just curious if there's a fundamental reason this isn't working. If I disable the WebServer that is served over an AccessPoint, the WebSocket transmission is successful. If I delay the initiation of the WebServer and AccessPoint, the WebSocket transmission is successful all the way up until the WebServer and AccessPoint are created.

    The main goal is to accept data from a non-Espruino device (plain Arduino) via an HTTP request to the WebServer on the Espruino board, and then relay that data via WebSockets to the remote server. I'm not sure if this is the best way of achieving this, any guidance is appreciated.

  • I suggest you post a simplied version of your code.

  • Sorry for the delayed response, I was dissecting the code and finding the exact moment where it goes wrong. I seem to have found that as soon as wifi.setAP() is called, it prevents execution of all socket transmissions, even those which are called previous to wifi.setAP().

    In the callbacks below, if you halt execution of Setup_AccessPoint() so that wifi.setAP() is never called, the WS.send() functions are successful. However, if Setup_AccessPoint() is called, no previous or subsequent WS.send() calls are successful.

    Seems weird that the previous ones don't work either, but the server never receives the data if wifi.setAP() is called.

    function onInit() {  
      /***************************************­*****************************
       * Import modules
       ****************************************­****************************/
      var 
        wifi = require('Wifi'),
        http = require('http'), 
        WebSocket = require("ws"),
        WebServer = require('WebServer');
      
      /***************************************­*****************************
       * Declare globals & functions
       ****************************************­****************************/
      var
        WS, // stores the WebSocket
        Setup_WiFi = function(callback) {
          console.log('Setup_WiFi is initiating.');
          wifi.connect('MY_WIFI', {password: 'MY_PASSWORD'}, Validate_WiFi(callback)); 
        },
        Validate_WiFi = function(callback) {
          if ( wifi.getIP().ip == '0.0.0.0' ) {
            console.log('[Notice][Validate_WiFi : Connection failed]');
            setTimeout(function(){
              Setup_WiFi(callback);
            } , 1500);
            return;
          } 
          
          console.log('[Notice][Validate_WiFi : Connection successful]');      
          callback();
        },
        Setup_Socket = function(callback) {
          console.log('Setup_Socket is initiating.');
          
          WS = new WebSocket( 'SOCKET_IP' , {
            port: SOCKET_PORT,
            keepAlive: 5000
          });
          
          WS.on('error', function() {
            console.log('Socket error has occurred');
          });
    
          WS.on('open', function() {
            console.log("Connected to socket");
            callback();
          });
    
          WS.on('message', function(data) {
            data = JSON.parse(data);
            console.log('Received data: ');
            console.log(data);
          });
          
        },
        Setup_AccessPoint = function(callback) {
          console.log('Creating AccessPoint.');
          
          
          // ###
          // As soon as the wifi.startAP() is called, no previous 
          // or subsequent socket transmission are successful.
          // ###
          
          wifi.startAP('Sample AP', {}, function(err) {
            // If this point is reached, socket will not work.
            if (err) throw err;
            console.log('AccessPoint is live.');
            callback();
          });
          
        };
    
      
      /***************************************­*****************************
       * Program procedure index
       * 1. Connect to WiFi
       * 2. Connect to WebSocket
       * 3. Create Access Point
       ****************************************­****************************/
      
      /***************************************­*****************************
       * Wait for WiFi connection before proceeding
       ****************************************­****************************/
      Setup_WiFi(function(){
        console.log('Setup_WiFi has finished.');
        Setup_Socket(function(){
          WS.send('Sample data');
          console.log('Setup_Socket has finished.');
          Setup_AccessPoint(function(){
            WS.send('Sample data #2');
            console.log('Setup_AccessPoint has finished.');
          });
        });
      });
      
    }  
    save();
    
  • Why don't you call Setup_AccessPoint() first, and then set up the sockets?

  • If the AP is setup before the WebSocket connection, it fails to connect.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Using WebSockets Client + WebServer module in the same program causes error "This socket is closed"

Posted by Avatar for espftw @espftw

Actions