Tornado implements a message wall.

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.