ESP8266 websockets client and server on same ESP

Hi all

I'm hoping someone can help me as I can't see where I am going wrong. I'm hoping I'm doing something wrong with the code.

I am trying to run both the webSockets Client and server in the same ESP8266.

I am using the links2000 library.

A browser webSocket client(A in diagram) will be used to connect to an ESP webSocket server(B in diagram) and then the same ESP server (B in diagram) will connect as a client to another websockets server(C in the diagram --I am using another ESP to test with).

The code compiles fine but I receive nothing but a connect at C. Zero text is coming through.

[0] Connected from 192.168.1.125 url: /

I only need the B to C connection to be up long enough to send a packet through.

I would also like to be able to enable it when needed (as in code above) and disable/disconnect when done)

Code below:

#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <WebSocketsServer.h>
#include <WebSocketsClient.h>
#include <Hash.h>

//ESP8266WiFiMulti WiFiMulti;
// Replace with your network credentials
const char* ssid = "my_ap";
const char* password = "password";
WebSocketsClient webSocket;
WebSocketsServer webSocket_server = WebSocketsServer(81);

boolean Master = true;

#define USE_SERIAL Serial

/***************************** SERVER EVENT handler *****************************************/

void webSocketEvent_server(uint8_t num, WStype_t type, uint8_t * payload, size_t lenght) {

  switch (type) {
    case WStype_DISCONNECTED:
      USE_SERIAL.printf("[%u] Disconnected!\n", num);
      break;
    case WStype_CONNECTED:
      {
        IPAddress ip = webSocket_server.remoteIP(num);
        USE_SERIAL.printf("[%u] Connected from %d.%d.%d.%d url: %s\n", num, ip[0], ip[1], ip[2], ip[3], payload);

        // send message to client
        webSocket_server.sendTXT(num, "Connected");
      }
      break;
    case WStype_TEXT:
      USE_SERIAL.printf("Received WStype_TEXT");
      USE_SERIAL.printf("[%u] get Text: %s\n", num, payload);
      if (payload[0] == '#') {
        if (Master) {
          USE_SERIAL.printf("I am devMaster ..server B ");
          webSocket.begin("192.168.1.126", 81);//open a client connection
          //webSocket.setAuthorization("user", "Password"); // HTTP Basic Authorization
          webSocket.onEvent(webSocketEvent); //fire up event handler
          webSocket.sendTXT("hello from Server as Client");
          webSocket.loop();
        }//end if Master
        else {
          USE_SERIAL.printf("I'm a Slave");
        }

      }
      USE_SERIAL.printf("end of WStype_TEXT receive");
      break;
    case WStype_BIN:
      USE_SERIAL.printf("[%u] get binary lenght: %u\n", num, lenght);
      hexdump(payload, lenght);

      // send message to client
      // webSocket_server.sendBIN(num, payload, lenght);
      break;
  }

}

/******************************* CLIENT EVENT handler*****************************************/

void webSocketEvent(WStype_t type, uint8_t * payload, size_t lenght) {
  Serial.println("Now  in client event handler");
  switch (type) {
    case WStype_DISCONNECTED:
      USE_SERIAL.printf("[WSc] Disconnected!\n");
      break;
    case WStype_CONNECTED:
      {
        USE_SERIAL.printf("[WSc] Client (B) Connected to url: %s\n",  payload);

        // send message to server when Connected
        webSocket.sendTXT("I'm a client Connected");
      }
      break;
    case WStype_TEXT:
      USE_SERIAL.printf("[WSc] get text: %s\n", payload);

      // send message to server
      webSocket.sendTXT("Hello this is a message for you server ");
      break;
    case WStype_BIN:
      USE_SERIAL.printf("[WSc] get binary lenght: %u\n", lenght);
      hexdump(payload, lenght);

      // send data to server
      // webSocket.sendBIN(payload, lenght);
      break;
  }

}

void setup() {
  // USE_SERIAL.begin(921600);
  USE_SERIAL.begin(115200);

  //Serial.setDebugOutput(true);
  // USE_SERIAL.setDebugOutput(true);
  // Serial.setDebugOutput(true);

  USE_SERIAL.println();
  USE_SERIAL.println();
  USE_SERIAL.println();

  for (uint8_t t = 4; t > 0; t--) {
    USE_SERIAL.printf("[SETUP] BOOT WAIT %d...\n", t);
    USE_SERIAL.flush();
    delay(1000);
  }
  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password); //begin WiFi connection
  Serial.println("");

  // Wait for connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.print("Connected to ");
  Serial.println(ssid);
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());


  webSocket_server.begin();
  webSocket_server.onEvent(webSocketEvent_server);

}

void loop() {
  // webSocket.loop();
  webSocket_server.loop();
}

DIAGRAM :

please see attached image if not displaying

Hi all

Just hoping if anyone could help with an update or help with this ... still having same issue :frowning: