title: Why does this simple websocket code throw when the client disconnects?
body: <p>I'm writing some simple web socket code based on <a href="https://websockets.readthedocs.io/en/stable/intro.html" rel="nofollow noreferrer">these docs</a>. The server is expected to listen for web sockets and just respond to <code>ping</code> messages with <code>pong</code>.</p>
  
  <p>I'm running on Python3 and the server code looks like this:</p>
  
  <pre><code>import asyncio
  import websockets
  from jsonrpcserver.aio import methods
  from jsonrpcserver.response import NotificationResponse
  
  @methods.add
  async def ping():
      return 'pong'
  
  async def accept_connection(websocket, path):
      async for request in websocket:
          response = await methods.dispatch(request)
          if not response.is_notification:
              await websocket.send(str(response))
  
  start_server = websockets.serve(accept_connection, 'localhost', 5000)
  asyncio.get_event_loop().run_until_complete(start_server)
  asyncio.get_event_loop().run_forever()
  </code></pre>
  
  <p>The client is in Dart and just calls <code>ping</code> twice then closes the connection. The output from the server gets this error when the client disconnects:</p>
  
  <pre><code>Dannys-MacBook:pythonws danny$ python3 server.py 
  --&gt; {"jsonrpc":"2.0","method":"ping","id":0}
  &lt;-- {"jsonrpc": "2.0", "result": "pong", "id": 0}
  --&gt; {"jsonrpc":"2.0","method":"ping","id":1}
  &lt;-- {"jsonrpc": "2.0", "result": "pong", "id": 1}
  Error in connection handler
  Traceback (most recent call last):
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/websockets/server.py", line 152, in handler
      yield from self.ws_handler(self, path)
    File "server.py", line 11, in accept_connection
      async for request in websocket:
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/websockets/py36/protocol.py", line 15, in __aiter__
      yield await self.recv()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/websockets/protocol.py", line 350, in recv
      yield from self.ensure_open()
    File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/websockets/protocol.py", line 512, in ensure_open
      self.close_code, self.close_reason) from self.transfer_data_exc
  websockets.exceptions.ConnectionClosed: WebSocket connection is closed: code = 1005 (no status code [internal]), no reason
  </code></pre>
  
  <p>According to the docs linked above:</p>
  
  <blockquote>
    <p>Iteration terminates when the client disconnects.</p>
  </blockquote>
  
  <p>This leads me to believe it should just exit the <code>async for</code> loop and not throw?</p>