Import Tornado.webImport Tornado.websocketImport tornado.httpserverimport tornado.ioloopimport tornado.optionsfrom uuid Import uuid4class ShoppingCart (object): Totalinventory = callbacks = [] Carts = {} def register (self, Callback): Self.callbacks.append (callback ) def unregister (self, callback): Self.callbacks.remove (callback) def Moveitemtocart (self, session): If session in Self.carts:return self.carts[session] = True self.notifycallbacks () def Removeitemfromcart (self, session): If session isn't in Self.carts:return del (Self.carts[session]) Self.notifycallbacks () def notifycallbacks (self): for callback in Self.callbacks:callback (sel F.getinventorycount ()) def getinventorycount (self): return Self.totalinventory-len (Self.carts) class Detailhand Ler (Tornado.web.RequestHandler): Def get (self): Session = Uuid4 () Count = Self.application.shoppingcart.g Etinventorycount () Self.render ("index.html", Session=session, Count=count) class Carthandler (Tornado.web.RequestHandler): Def post (self ): action = self.get_argument (' action ') session = Self.get_argument (' session ') if not session: Self.set_status (+) return if action = = ' Add ': self.application.shoppingCart.moveItemTo Cart (session) elif action = = ' Remove ': Self.application.shoppingCart.removeItemFromCart (session) Else:self.set_status (400)class Statushandler (Tornado.websocket.WebSocketHandler): Def open (self): Self.application.shoppingCart.regis Ter (self.callback) def on_close (self): Self.application.shoppingCart.unregister (self.callback) def on_message (Self, message): Pass Def callback (self, Count): Self.write_message (' {"Inventorycount": "%d"} '% c Ount)Class Application (tornado.web.Application): def __init__ (self): Self.shoppingcart = ShoppingCart () handle rs = [(R '/', Detailhandler), (R '/cart ', Carthandler),(R '/cart/status ', Statushandler)] Settings = {' Template_path ': ' Templates ', ' Static_path ': ' Static '} tornado. Web. Application.__init__ (self, handlers, **settings) if __name__ = = ' __main__ ': Tornado.options.parse_command_line () app = Application () Server = Tornado.httpserver.HTTPServer (APP) Server.listen (8000) tornado.ioloop.IOLoop.instance (). Start ()
tornado in websocket module provides a websockethandler class. This class provides hooks for websocket events and methods that communicate with connected clients. When a new WebSocket connection is opened, open method was called, and on_message and on_close method is called when the connection receives a new message and the client shuts down.
In addition, the WebSocketHandler class provides write_message methods for sending messages to clients, which are close used to close the connection.
Class Echohandler (Tornado.websocket.WebSocketHandler): def Open (self): self.write_message (' connected! ') def on_message (self, message): self.write_message (Message)
As you EchoHandler can see in our implementation, the open method simply WebSocketHandler sends the write_message string "connected!" to the client using the method provided by the base class. Each time the handler receives a new message from the client, the on_message method is called, and our implementation returns the client-provided message to the client as it was
Implement WebSocket with Tornado