Last active
January 8, 2016 02:39
-
-
Save davidbj/6c9292ece8d657167bcd to your computer and use it in GitHub Desktop.
python tornado websocket demo.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #实现一个简单的python tornado Websocket 实例. | |
| #${ROOT}/ws.py | |
| #!-*- coding:utf-8 -*- | |
| import os.path | |
| import tornado.httpserver | |
| import tornado.web | |
| import tornado.ioloop | |
| import tornado.options | |
| import tornado.httpclient | |
| import tornado.websocket | |
| import json | |
| class IndexHandler(tornado.web.RequestHandler): | |
| def get(self): | |
| self.render("index.html") | |
| class SocketHandler(tornado.websocket.WebSocketHandler): | |
| clients = set() | |
| @staticmethod | |
| def send_to_all(message): | |
| #向每一个客户端发送消息. | |
| for c in SocketHandler.clients: | |
| c.write_message(json.dumps(message)) | |
| def open(self): | |
| '''当客户端连接Websocket服务器时触发open方法.''' | |
| #客户端首次登入时,Websocket Server向客户端发送消息内容. | |
| self.write_message(json.dumps({ | |
| 'type': 'sys', | |
| 'message': str(id(self)) + ' has joined', | |
| })) | |
| #将每次登入的客户端,加入到一个集合. | |
| SocketHandler.clients.add(self) | |
| def on_close(self): | |
| '''关闭客户端浏览器时之行的动作.''' | |
| #当客户端关闭,从clients集合中删除该客户. | |
| SocketHandler.clients.remove(self) | |
| #同时通知还在clients集合里的其它客户端用户. | |
| SocketHandler.send_to_all({ | |
| 'type': 'sys', | |
| 'message': str(id(self)) + ' has left', | |
| }) | |
| def on_message(self, message): | |
| '''收到信息时候进行的动作.''' | |
| #当收到客户端发送的Request信息时,Websocket Server向Clients | |
| #集合发送信息. | |
| SocketHandler.send_to_all({ | |
| 'type': 'user', | |
| 'id': id(self), | |
| 'message': message, | |
| }) | |
| if __name__ == '__main__': | |
| app = tornado.web.Application( | |
| handlers = [ | |
| (r"/", IndexHandler), | |
| (r"/chat", SocketHandler) | |
| ], | |
| debug = True, | |
| template_path = os.path.join(os.path.dirname(__file__), "templates"), | |
| static_path = os.path.join(os.path.dirname(__file__), "static") | |
| ) | |
| app.listen(8200) | |
| tornado.ioloop.IOLoop.instance().start() | |
| #${ROOT}/templates/index.html | |
| <html> | |
| <head> | |
| <script type="text/javascript"> | |
| var ws = new WebSocket("ws://localhost:8200/chat"); | |
| ws.onmessage = function(event) { | |
| //console.log(event); | |
| var table = document.getElementById('content'); | |
| table.insertRow().insertCell().innerHTML= event.data; | |
| } | |
| function send(){ | |
| ws.send(document.getElementById('chat').value); | |
| } | |
| </script> | |
| </head> | |
| <body> | |
| <div> | |
| hello | |
| <input id="chat"> | |
| <button onclick="send()">send</button> | |
| </div> | |
| <table id="content"> | |
| </table> | |
| </body> | |
| </html> | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment