• 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
        wifi = require('Wifi'),
        http = require('http'), 
        WebSocket = require("ws"),
        WebServer = require('WebServer');
       * Declare globals & functions
        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 == '' ) {
            console.log('[Notice][Validate_WiFi : Connection failed]');
            } , 1500);
          console.log('[Notice][Validate_WiFi : Connection successful]');      
        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");
          WS.on('message', function(data) {
            data = JSON.parse(data);
            console.log('Received 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.');
       * Program procedure index
       * 1. Connect to WiFi
       * 2. Connect to WebSocket
       * 3. Create Access Point
       * Wait for WiFi connection before proceeding
        console.log('Setup_WiFi has finished.');
          WS.send('Sample data');
          console.log('Setup_Socket has finished.');
            WS.send('Sample data #2');
            console.log('Setup_AccessPoint has finished.');
  • 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

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

Posted by Avatar for espftw @espftw