title: Handshake Issues Tornado Server and ESP32 Client
body: <p>I am unable to establish a websocket connection. I use an esp32 as an Client and an Tornado Webserver as Host.
  I can connect to the Host with Javascript, but I can also connect the esp32 to websocket.org.
  Only the connection between the esp32 and the Tornado Server doesn't work.</p>
  <p>ESP32 Code (Arduino IDE):</p>
  <pre><code>#include &lt;WiFi.h&gt;
  #include &lt;WebSocketClient.h&gt; //By Markus Sattler
  
  const char* ssid     = &quot;hadome&quot;;
  const char* password = &quot;12345678&quot;;
  
  char path[] = &quot;/ws&quot;;
  char host[] = &quot;192.168.178.29&quot;;
   
  WebSocketClient webSocketClient;
  WiFiClient client;
   
  void setup() {
    Serial.begin(115200);
   
    WiFi.begin(ssid, password);
   
    while (WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(&quot;.&quot;);
    }
   
    Serial.println(&quot;&quot;);
    Serial.println(&quot;WiFi connected&quot;);
    Serial.println(&quot;IP address: &quot;);
    Serial.println(WiFi.localIP());
   
    delay(5000);
   
    if (client.connect(host, 3333)) {
      Serial.println(&quot;Connected&quot;);
    } else {
      Serial.println(&quot;Connection failed.&quot;);
    }
   
    webSocketClient.path = path;
    webSocketClient.host = host;
    
    if (webSocketClient.handshake(client)) {
      Serial.println(&quot;Handshake successful&quot;);
    } else {
      Serial.println(&quot;Handshake failed.&quot;);
    }
   
  }
   
  void loop() {
    String data;
   
    if (client.connected()) {
   
      webSocketClient.sendData(&quot;Info to be echoed back&quot;);
   
      webSocketClient.getData(data);
      if (data.length() &gt; 0) {
        Serial.print(&quot;Received data: &quot;);
        Serial.println(data);
      }
   
    } else {
      Serial.println(&quot;Client disconnected.&quot;);
    }
   
    delay(3000);
   
  }
  </code></pre>
  <p>Python Code Excerpt (Tornado):</p>
  <pre><code>class WSHandler(tornado.websocket.WebSocketHandler):
      def check_origin(self, origin):
          return True
  
      def open(self):
          pass
  
      def on_message(self, message):
          self.write_message(message)
          
  
      def on_close(self):
          log('[WS] Connection was closed.')
  
  application    = tornado.web.Application([(r'/ws', WSHandler)])
  
  # Main program logic follows:
  if __name__ == '__main__':
  
      # Tornado Server
      try:
          http_server = tornado.httpserver.HTTPServer(application)
          http_server.listen(3333)
  
          http_client = tornado.httpclient.AsyncHTTPClient()
          http_client.initialize(1)
  
          log(&quot;Tornado Server started&quot;)
  
  
          tornado.ioloop.IOLoop.current().spawn_callback(loop)
          tornado.ioloop.IOLoop.instance().start()
      except:
          log(&quot;Exception triggered - Tornado Server stopped.&quot;)
  
      finally:
          pass
  </code></pre>
  <p>The ESP32 Program outputs:</p>
  <pre><code>.......
  WiFi connected
  IP address: 
  192.168.2.8
  Connected
  Waiting...
  Handshake failed.
  
  </code></pre>
  <p>I am very confused because this javascript program works flawlessly (<a href="https://www.mischianti.org/2020/12/07/websocket-on-arduino-esp8266-and-esp32-client-1/" rel="nofollow noreferrer">https://www.mischianti.org/2020/12/07/websocket-on-arduino-esp8266-and-esp32-client-1/</a>):</p>
  <pre><code>var wsUri = &quot;ws://192.168.178.29:3333/ws&quot;;
  var output;
  
  function init() {
      output = document.getElementById(&quot;output&quot;);
      testWebSocket();
  }
  
  function testWebSocket() {
      websocket = new WebSocket(wsUri);
      websocket.onopen = function (evt) {
         onOpen(evt)
      };
      websocket.onclose = function (evt) {
         onClose(evt)
      };
      websocket.onmessage = function (evt) {
         onMessage(evt)
      };
      websocket.onerror = function (evt) {
         onError(evt)
      };
  }
  </code></pre>
  <p>Sincerely Hannes</p>
  <p>PS: Sorry for my bad english</p>