title: tornado - WebSocketClientConnection - How to catch and handle connection failures?
body: <p>I am a Tornado and also Websocket newbie. There are many resources how to implement a websocket server application with Tornado. However, I haven't found a complete example that contains a websocket client application built on top of Tornado.</p>
  
  <hr>
  
  <h1>Server Application (server.py)</h1>
  
  <pre><code>#!/usr/bin/env python
  # -*- coding: utf-8 -*-
  
  import logging
  import tornado.web
  import tornado.websocket
  import tornado.ioloop
  import tornado.options
  
  from tornado.options import define, options
  
  define("port", default=3000, help="run on the given port", type=int)
  
  
  class Application(tornado.web.Application):
      def __init__(self):
          handlers = [(r"/", MainHandler)]
          settings = dict(debug=True)
          tornado.web.Application.__init__(self, handlers, **settings)
  
  
  class MainHandler(tornado.websocket.WebSocketHandler):
      def check_origin(self, origin):
          return True
  
      def open(self):
          logging.info("A client connected.")
  
      def on_close(self):
          logging.info("A device disconnected")
  
  
  def main():
      tornado.options.parse_command_line()
      app = Application()
      app.listen(options.port)
      tornado.ioloop.IOLoop.instance().start()
  
  
  if __name__ == "__main__":
      main()
  </code></pre>
  
  <hr>
  
  <h1>Client Application (client.py):</h1>
  
  <pre><code>#!/usr/bin/env python
  # -*- coding: utf-8 -*-
  
  from tornado.ioloop import IOLoop
  from tornado.websocket import websocket_connect
  
  
  if __name__ == "__main__":
      ws = websocket_connect("ws://localhost:3000")
      IOLoop.instance().start()
  </code></pre>
  
  <hr>
  
  <h1>Problems &amp; Questions</h1>
  
  <p>I don't have any problems with server application. However, I can't say same thing for client application. Why?</p>
  
  <ol>
  <li>If server goes down, client application doesn't react. It does not say "Hey, server is down buddy.". I write my own WebSocketClientConnection class (inherited by the original) and also websocket_connect function (same function, but this one uses my WebSocketClientConnection class). I override on_connection_close method in my WebSocketClientConnection class, so I can catch "server down on middle of the connection" failure. However, I cannot make it reconnect. How do I make it reconnect to server?</li>
  <li>If server is already down before the connection, client application does not raise anything and seems perfectly. I don't know how to catch that failure. How do I catch this connection failure?</li>
  </ol>
  
  <p>My WebSocketClientConnection class:</p>
  
  <pre><code>class MyWebSocketClientConnection(tornado.websocket.WebSocketClientConnection):
      def on_connection_close(self):
          super(MyWebSocketClientConnection, self).on_connection_close()
          print "connection closed"
  </code></pre>
  
  <p>Thanks.</p>