title: how to close a websocket like if chrome window was closing to flush the buffer to 0
body: <p>when with portfowarding the websocket pass by internet and only send when the connexion is close  (on my network it's like that), I first try to close the websocket by closing chrome(it worked), so now i found terminate that i think can do the same than closing the webbrowser,but it's only for the serveur that have this function and</p>
  <p>the problem is that the buffer with close is not 0 but 13...</p>
  <p><em><strong>so how can i do a <code>hard close</code>  with <code>ws.terminate</code> or something else to flush the buffer to 0 and a basic code like the one under without having the error?</strong></em></p>
  <pre><code>ws = new WebSocket(&quot;ws://adress:5677/&quot;),
  messages = document.createElement('ul');
  ws.onopen = function (event) {
              alert(&quot;connect&quot;);
              var lol = document.getElementById('lolz').value;
                alert(lol);
              ws.send(lol+&quot; &lt;p&gt;\b\n\r&quot;);
              ws.close();
              console.log(ws.readyState);
           
              
              
              if(ws.readyState ==2){
                  alert(&quot;ws.terminate&quot;);
                  
                  return ws.terminate();
                 
          
         
              }
  }
  </code></pre>
  <p>if you look the picture and the code you can see that i use<code>ws.close</code> but it doesn't seems to do something the buffer is not to zero and it's still connected this is a pictures of what is the status of the Websocket after ws.close(), i think that ws.close() waits that the serveur also close the connection and when the serveur waits to receive something before closing the serveur, it put's an error.</p>
  <p>my pthon serveur if never</p>
  <pre><code>async def Time(websocket, path):
      
      
      try:
          a=await websocket.recv()
          print(&quot;final receive:&quot;+str(a)+&quot;/&quot;)
          a=str(a).split(&quot;&lt;p&gt;&quot;)
          a=a[0]
          print(&quot;/&quot;+str(a)+&quot;/&quot;)
      except:
          print(&quot;erreur&quot;)
      finally:
          socketout(bytes(a.encode(&quot;utf-8&quot;)))
          print(&quot;loop stop&quot;)
          loop.stop()
          print(&quot;loop ad stop&quot;)
          
  start_server = websockets.serve(Time, &quot;&quot;, 5678)
  loop3=asyncio.get_event_loop()   
  
  loop3.run_until_complete(start_server)
  </code></pre>
  <p><em><strong>a visual explication</strong></em>
  Thanks for helping
  [![visual][2]][2]</p>