user71306: I am writing a program that echos back the message sent to the web socket server on a host machine. When running the ESP module, the connection is established but immediately disconnects as seen in the first image. I originally thought this maybe a result to the host machines firewall, however, using my laptops connection, I was able to produce the desired echo. Is the constant disconnect a result in different protocols or something I am missing?
WebSocketServer on host machine (modeled off example)
import asyncio

from autobahn.asyncio.websocket import WebSocketServerProtocol, \
    WebSocketServerFactory


class MyServerProtocol(WebSocketServerProtocol):

    def onConnect(self, request):
        print("Client connecting: {0}".format(request.peer))

    def onOpen(self):
        print("WebSocket connection open.")

    def onMessage(self, payload, isBinary):
        if isBinary:
            print("Binary message received: {0} bytes".format(len(payload)))
        else:
            print("Text message received: {0}".format(payload.decode('utf8')))

        # echo back message verbatim
        self.sendMessage(payload, isBinary)

    def onClose(self, wasClean, code, reason):
        print("WebSocket connection closed: {0}".format(reason))


if __name__ == '__main__':
    factory = WebSocketServerFactory("ws://127.0.0.1:9000")
    factory.protocol = MyServerProtocol

    loop = asyncio.get_event_loop()
    coro = loop.create_server(factory, '0.0.0.0', 9000)
    server = loop.run_until_complete(coro) 

    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass
    finally:
        server.close()
        loop.close()

ESP8266 modeled in arduinoWebSockets
#include <Arduino.h>

#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>

#include <ArduinoJson.h>

#include <WebSocketsClient.h>
#include <SocketIOclient.h>

#include <Hash.h>

ESP8266WiFiMulti wiFiMulti;
SocketIOclient socketIO;

void socketIOEvent(socketIOmessageType_t type, uint8_t * payload, size_t length) {
    switch(type) {
        case sIOtype_DISCONNECT:
            Serial.printf("[IOc] Disconnected!\n");
            break;
        case sIOtype_CONNECT:
            Serial.printf("[IOc] Connected to url: %s\n", payload);
            socketIO.send(sIOtype_CONNECT, "/");
            break;
        case sIOtype_EVENT:
            Serial.printf("[IOc] get event: %s\n", payload);
            break;
        case sIOtype_ACK:
            Serial.printf("[IOc] get ack: %u\n", length);
            hexdump(payload, length);
            break;
        case sIOtype_ERROR:
            Serial.printf("[IOc] get error: %u\n", length);
            hexdump(payload, length);
            break;
        case sIOtype_BINARY_EVENT:
            Serial.printf("[IOc] get binary: %u\n", length);
            hexdump(payload, length);
            break;
        case sIOtype_BINARY_ACK:
            Serial.printf("[IOc] get binary ack: %u\n", length);
            hexdump(payload, length);
            break;
    }
}

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

    //Serial.setDebugOutput(true);
    Serial.setDebugOutput(true);
    Serial.printf("[SETUP] starting up");

    Serial.println();
    Serial.println();
    Serial.println();

      for(uint8_t t = 4; t > 0; t--) {
          Serial.printf("[SETUP] BOOT WAIT %d...\n", t);
          Serial.flush();
          delay(1000);
      }

    // disable AP
    if(WiFi.getMode() & WIFI_AP) {
        WiFi.softAPdisconnect(true);
    }

    wiFiMulti.addAP("ssid", "password");

    //WiFi.disconnect();
    while(wiFiMulti.run() != WL_CONNECTED) {
        delay(5000);
        Serial.println("Retrying...");
    }

    String ip = WiFi.localIP().toString();
    Serial.printf("[SETUP] WiFi Connected %s\n", ip.c_str());

    // server address, port and URL
    socketIO.begin("local host address", 9000);
    // event handler
    socketIO.onEvent(socketIOEvent);
}

unsigned long messageTimestamp = 0;
//int attempts = 0;
void loop() {
    socketIO.loop();
}




user71306: After testing the server on the host machine using an additional computer, I was able to produce the desired output. I would suspect it is an issue within the library or mis-use/understanding. I found a post that suggested the library was the problem and ended up using this library instead.
Using the example implementation , I was able to achieve the desired results.

71306: <p>I am writing a program that echos back the message sent to the web socket server on a host machine. When running the ESP module, the connection is established but immediately disconnects as seen in the first image. I originally thought this maybe a result to the host machines firewall, however, using my laptops connection, I was able to produce the desired echo. Is the constant disconnect a result in different protocols or something I am missing?</p>
<p>WebSocketServer on host machine (modeled off example)</p>
<pre><code>import asyncio

from autobahn.asyncio.websocket import WebSocketServerProtocol, \
    WebSocketServerFactory


class MyServerProtocol(WebSocketServerProtocol):

    def onConnect(self, request):
        print(&quot;Client connecting: {0}&quot;.format(request.peer))

    def onOpen(self):
        print(&quot;WebSocket connection open.&quot;)

    def onMessage(self, payload, isBinary):
        if isBinary:
            print(&quot;Binary message received: {0} bytes&quot;.format(len(payload)))
        else:
            print(&quot;Text message received: {0}&quot;.format(payload.decode('utf8')))

        # echo back message verbatim
        self.sendMessage(payload, isBinary)

    def onClose(self, wasClean, code, reason):
        print(&quot;WebSocket connection closed: {0}&quot;.format(reason))


if __name__ == '__main__':
    factory = WebSocketServerFactory(&quot;ws://127.0.0.1:9000&quot;)
    factory.protocol = MyServerProtocol

    loop = asyncio.get_event_loop()
    coro = loop.create_server(factory, '0.0.0.0', 9000)
    server = loop.run_until_complete(coro) 

    try:
        loop.run_forever()
    except KeyboardInterrupt:
        pass
    finally:
        server.close()
        loop.close()
</code></pre>
<p>ESP8266 modeled in <a href="https://github.com/Links2004/arduinoWebSockets/blob/master/examples/esp8266/WebSocketClientSocketIO/WebSocketClientSocketIO.ino" rel="nofollow noreferrer">arduinoWebSockets</a></p>
<pre><code>#include &lt;Arduino.h&gt;

#include &lt;ESP8266WiFi.h&gt;
#include &lt;ESP8266WiFiMulti.h&gt;

#include &lt;ArduinoJson.h&gt;

#include &lt;WebSocketsClient.h&gt;
#include &lt;SocketIOclient.h&gt;

#include &lt;Hash.h&gt;

ESP8266WiFiMulti wiFiMulti;
SocketIOclient socketIO;

void socketIOEvent(socketIOmessageType_t type, uint8_t * payload, size_t length) {
    switch(type) {
        case sIOtype_DISCONNECT:
            Serial.printf(&quot;[IOc] Disconnected!\n&quot;);
            break;
        case sIOtype_CONNECT:
            Serial.printf(&quot;[IOc] Connected to url: %s\n&quot;, payload);
            socketIO.send(sIOtype_CONNECT, &quot;/&quot;);
            break;
        case sIOtype_EVENT:
            Serial.printf(&quot;[IOc] get event: %s\n&quot;, payload);
            break;
        case sIOtype_ACK:
            Serial.printf(&quot;[IOc] get ack: %u\n&quot;, length);
            hexdump(payload, length);
            break;
        case sIOtype_ERROR:
            Serial.printf(&quot;[IOc] get error: %u\n&quot;, length);
            hexdump(payload, length);
            break;
        case sIOtype_BINARY_EVENT:
            Serial.printf(&quot;[IOc] get binary: %u\n&quot;, length);
            hexdump(payload, length);
            break;
        case sIOtype_BINARY_ACK:
            Serial.printf(&quot;[IOc] get binary ack: %u\n&quot;, length);
            hexdump(payload, length);
            break;
    }
}

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

    //Serial.setDebugOutput(true);
    Serial.setDebugOutput(true);
    Serial.printf(&quot;[SETUP] starting up&quot;);

    Serial.println();
    Serial.println();
    Serial.println();

      for(uint8_t t = 4; t &gt; 0; t--) {
          Serial.printf(&quot;[SETUP] BOOT WAIT %d...\n&quot;, t);
          Serial.flush();
          delay(1000);
      }

    // disable AP
    if(WiFi.getMode() &amp; WIFI_AP) {
        WiFi.softAPdisconnect(true);
    }

    wiFiMulti.addAP(&quot;ssid&quot;, &quot;password&quot;);

    //WiFi.disconnect();
    while(wiFiMulti.run() != WL_CONNECTED) {
        delay(5000);
        Serial.println(&quot;Retrying...&quot;);
    }

    String ip = WiFi.localIP().toString();
    Serial.printf(&quot;[SETUP] WiFi Connected %s\n&quot;, ip.c_str());

    // server address, port and URL
    socketIO.begin(&quot;local host address&quot;, 9000);
    // event handler
    socketIO.onEvent(socketIOEvent);
}

unsigned long messageTimestamp = 0;
//int attempts = 0;
void loop() {
    socketIO.loop();
}
</code></pre>
<p><a href="https://i.stack.imgur.com/AKIR1.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/AKIR1.png" alt="Serial Monitor Output of the ESP8266 " /></a>
<a href="https://i.stack.imgur.com/O7cR9.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/O7cR9.png" alt="Connection output from Python WSS" /></a></p>
 71306: <p>After testing the server on the host machine using an additional computer, I was able to produce the desired output. I would suspect it is an issue within the library or mis-use/understanding. I found a <a href="https://forum.arduino.cc/index.php?topic=522526.0" rel="nofollow noreferrer">post</a> that suggested the library was the problem and ended up using this <a href="https://github.com/gilmaimon/ArduinoWebsockets" rel="nofollow noreferrer">library</a> instead.</p>
<p>Using the <a href="https://github.com/gilmaimon/ArduinoWebsockets/blob/master/examples/Esp8266-Client/Esp8266-Client.ino" rel="nofollow noreferrer">example implementation</a> , I was able to achieve the desired results.</p>
