Tornado has good support for asynchronous HTTP. So follow the demo and summarize what the next message wall is going to do.
Idea: First check the next there are two ideas, one is the client pull is a server push.
Using server pull, technology is a popular comet technology.
Comet probably says: "My client sends a request to the server side, then the server starts a wireless loop, puts the data clinet needs into response, and continues to refresh until the entire clinet connection to the server disconnects." So he's a technology based on long connections.
1. The first step is to send a new message processing, that is, a client sends a message, to broadcast the notification to all the client
1 classnewmessage (tornado.web.RequestHandler):2 #Overwrite Post method3 defpost (self):4 #define messages to send5Message = {6 "ID": Str (UUID.UUID4 ()),7 "Body", Self.get_argument ("Body"),8 }9message["HTML"] =tornado.escape.to_basestring (TenSelf.render_string ("message.html", message=message)) One ifSelf.get_argument ("Next", None): ASelf.redirect (Self.get_arugment ("Next")) - Else: - self.write (message) the #Global_message_buffer is a global var - -Global_message_buffer.new_message ([message])
Explanation: message[html] This part is to pass the message into the template and return the HTML code.
The most important thing is to broadcast the mesage. The global_message_buffer used.
Global_message_buffer = Messagebuffer ()
classMessagebuffe (): Self.__init__(self): Self.waiters=set () Self.cache=[] self.cache_size= 10#define max message Cacheself.new_message (self,message):#send message to waiters forFutureinchself.waiters:future.set_result (message)#update waiters to emptySelf.waiters =set ()#Update Cacheself.cache.extend (message)#Check Cache size ifLen (Self.cache) >Self.cache_size:self.cache= Self.cache[-self.cache_size:]
Come back and write.
Tornado implements a message wall.