title: connecting javascript secure websocket client to python websocket asyncio server
body: <p>I'm having trouble establishing connection between javascript websocket client and python server (with the asyncio and websocket module). When i used the simple websocket (unsecure) the connection is working fine but when i'm trying to use the secure websocket (wss) i get an error:</p>
  
  <pre><code>   WebSocket connection to 'wss://127.0.0.1:6169/' failed: Error in connection 
     establishment: net::ERR_SSL_PROTOCOL_ERROR
  </code></pre>
  
  <p><strong>my server code:</strong></p>
  
  <pre><code>async def app(websocket, path):
      while True :
          data = await websocket.recv()
          if (data== "close"):
              print("connection with client closed.")
              break
          data = data.encode()
          arr = data.split("~".encode())
          for i in range(0,4):
              arr[i]=arr[i].decode()
          resualt=algo(arr)
          await websocket.send(resualt)
  
  server = websockets.serve(app, '0.0.0.0', 6169)
  
  asyncio.get_event_loop().run_until_complete(server)
  asyncio.get_event_loop().run_forever()
  </code></pre>
  
  <p><strong>my client code:</strong></p>
  
  <pre><code>var st=password+"~"+url+"~"+clientid+"~"+option;
                      if ('WebSocket' in window){
                          var socket = new WebSocket("wss://127.0.0.1:6169/");
                          socket.onopen = function (evt) {
                              socket.send(st);
                          };
                          socket.onmessage = function (evt) {
                              alert("scrool extension page down to see the password");
                              $('#res').val(evt.data);
                              socket.send("close");
                          };
                          socket.onerror = function (evt) {
                              alert("the error is: "+evt.data);
                          };
                      }
                      else {
                         alert("web socket is not supported")
                      }
  </code></pre>
  
  <p>when i replaced this line : </p>
  
  <pre><code>    var socket = new WebSocket("wss://127.0.0.1:6169/");
  </code></pre>
  
  <p>with this one:</p>
  
  <pre><code>    var socket = new WebSocket("ws://127.0.0.1:6169/");
  </code></pre>
  
  <p>everthing is working fine.</p>
  
  <p>if somebody knows what is my mistake or how to succesfuly establish a secure connection between js client with websocket library and python server with asyncio and web socket module, please help. I have searched all over the web for an example that works but i couldn't find one.</p>
  
  <p>I also tried to enable TLS in my server  by changing my code to this :</p>
  
  <pre><code>    context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH) 
      context.load_default_certs(purpose=ssl.Purpose.CLIENT_AUTH) 
      server = websockets.serve(app, '0.0.0.0', 6169, ssl=context) 
  </code></pre>
  
  <p>but it still does'nt work and give me the following error: </p>
  
  <pre><code>    WebSocket connection to 'wss://127.0.0.1:6169/' failed: Error in 
      connection establishment: net::ERR_CONNECTION_CLOSED –
  </code></pre>