📜 ⬆️ ⬇️

Chatter - based on python 2.7 websocket-framework using tornado

I want to introduce my small creation to the community - Chatter.

Chatter is based on python 2.7, uses tornado .

There is a ready API for python (backend) and for js (frontend)
')
Examples and source code on github .

Why was it actually created?





For quick and easy development. There is own “protocol” - a wrapper over json.

The structure of the "app" file:

from chatter import BaseSocketHandler, run_application, Clients class Example1SocketHandler(BaseSocketHandler): #  namespace  -        group = 'example1' def main(): #Tornado-handler' run_application([ (r'/example1', Example1SocketHandler), ]) if __name__ == '__main__': main() 


Commands are stored next to the app files in the api folder. Folder structure:




Nothing more is needed - when the service starts, the api folder will be automatically analyzed for “methods”, the tree structure of the methods will be built.

Dynamic update of methods is supported (example in gita is chat).

Method file (/api/example1/hello/world.py):

 class __api_result__(APIMethod): #  ,     -  ""  def run(self, text1, text2, text3): #  - tuple #  - dict- # - success (  - True / False) return ({'text': [text1, text2, text3]}, True) 


And now the html-js-part:

 <!DOCTYPE html> <html> <head> <title>Chatter test</title> <meta charset="UTF-8"> <meta http-equiv="content-type" content="application/xhtml+xml; charset=utf-8"/> <!--  chatter.js --> <script src="js/vmchatter.js"></script> <script src="http://code.jquery.com/jquery.js"></script> <script> //      .   -  function Example1Chatter(host, port, params) { //  chatter',   , ,   "" this.chatter = new VMChatter(host, port, 'example1', params, this); } // ,   hello Example1Chatter.prototype.hello = function(text2, text1, text3, callback) { //  ,     , ,       this.chatter.applyMethod('hello', 'world', arguments); }; </script> <script type="text/javascript"> var sx; $(document).ready(function() { //,  sx = new Example1Chatter('localhost', 8888, { 'onOpen': function(){ console.log('Connected'); }, 'onClose': function(){ console.log('Disconnected'); } }); }); function sendHello() { sx.hello('text2', 'text1', 'text3', function(data, success) { $('#results').append('<p>' + data.text + '</p>'); }); } </script> </head> <body> <button class="btn" onclick="sendHello();">Send hello</button> <div id="results"></div </body> 


Subscription to events is also supported (mass mailing to subscribing, sending to a specific client).

At the moment, unfortunately, there is no documentation, but I am in the process of writing it. This framework is used in the company where I work. So it is “tested” on the load, some of the bugs were caught and corrected.

There are so many possibilities that are not presented here and in the examples, several more "buns" are being prepared.

Other examples and source code on github .

Source: https://habr.com/ru/post/184090/


All Articles