title: Waiting to recv() on two Websockets
body: <p>I'm trying to create essentially a websocket-to-websocket bridge/gateway. This server should accept public connections on 8765 and forwards messages to another internal server waiting on 7777, and vice versa.</p>
  
  <p>My question is how do I monitor for arriving messages from <em>either</em> socket? The code below is only looping over the public socket messages. I'm not sure how to be watching both the publicsock and publicsock.privatesock at the same time. If something happens on either socket, I need to forward the message to the other side.</p>
  
  <p>Originally from <a href="http://aaugustin.github.io/websockets/" rel="nofollow">http://aaugustin.github.io/websockets/</a></p>
  
  <pre><code>import asyncio
  import websockets
  
  @asyncio.coroutine
  def handler(publicsock, path):
      publicsock.privatesock = yield from websockets.connect('ws://localhost:7777/')
      while True:
          message = yield from publicsock.recv()
          if message is None:
              break
          yield from processPublicMessage(publicsock, message)
      publicsock.privatesock.close()
  
  start_server = websockets.serve(handler, 'localhost', 8765)
  asyncio.get_event_loop().run_until_complete(start_server)
  asyncio.get_event_loop().run_forever()
  </code></pre>