TornadoでWebSocketクライアントセッションを管理する

Tornadoでクライアント側からWebSocketでセッションを張る際にサーバーサイドでクライアントの情報を管理する。
"self"はクライアントごとに独自のSocketHandlerを返す。

#-*- coding:shift-jis -*-

import tornado.ioloop
from tornado.web import *
import tornado.websocket
import tornado.httpserver
class MainHandler(RequestHandler):
	def get(self):
		self.render("index.html")
cone = []
class SocketHandler(tornado.websocket.WebSocketHandler):
	def open(self):
		if self not in cone:
		 cone.append(self)
		print "-------------------"
		print "connected"
		print cone
		print "--------------------"
	def on_close(self):
		if self in cone:
			cone.remove(self)
		print "------------------"
		print "dis-connected"
		print cone
		print "-------------------"
	def on_message(self,message):
		for i in cone:
		 i.write_message(message)
app = Application([(r"/websocket",SocketHandler),(r"/",MainHandler)])
http_server = tornado.httpserver.HTTPServer(app)
http_server.listen(8000)
tornado.ioloop.IOLoop.instance().start()

index.htmlで同ディレクトリに保存

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<title></title>
</head>
<body>
 <script>
  var ws = new WebSocket("ws:localhost:8000/websocket")
   ws.onopen = function(){
	 ws.send("hello")
   }
    ws.onmessage=function(evt){alert(evt.data)}
   function c(ws){
     ws.send("oh,mygod")
    }
  </script>
  <form action="">
  <input type="button" text="submit"  onClick="c(ws)"/>
  </form>
</body>
</html>

以下のページが非常に参考になる。
http://masahito.hatenablog.com/entry/20100221/1266773601
https://sites.google.com/site/tornadowebja/documentation/integration-with-other-services/tornado-websocket
http://voluntas.hatenablog.com/entry/20100221/1266719424