Skip to content

wsrpc is WebSocket Remote procedure call for tornado and javascript

License

Notifications You must be signed in to change notification settings

michael-borisov/wsrpc

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

WSRPC Tornado

Remote Procedure call through WebSocket between browser and tornado.

Installation

Install via pip:

pip install wsrpc-tornado

Simple usage

Add the backend side:

    from time import time
    from wsrpc import WebSocketRoute, WebSocket, wsrpc_static

    tornado.web.Application((
    wsrpc_static(r'/js/(.*)'),
    (r"/ws/", WebSocket),
    (r'/(.*)', tornado.web.StaticFileHandler, {
         'path': os.path.join(project_root, 'static'),
         'default_filename': 'index.html'
    }),
))

    class TestRoute(WebSocketRoute):
            def init(self, **kwargs):
                    return kwargs

            def getEpoch(self):
                    return time()

    # stateful request
    WebSocket.ROUTES['test1'] = TestRoute

    # stateless request
    WebSocket.ROUTES['test2'] = lambda *a, **kw: True

Add the frontend side:

<script type="text/javascript" src="/js/q.min.js"></script>
<script type="text/javascript" src="/js/wsrpc.min.js"></script>
<script>
        var url = window.location.protocol==="https:"?"wss://":"ws://" + window.location.host + '/ws/';
        RPC = WSRPC(url, 5000);
        RPC.addRoute('test', function (data) { return "Test called"; });
        RPC.connect();

        RPC.call('test1.getEpoch').then(function (data) {
                console.log(data);
        }, function (error) {
                alert(error);
        }).done();

        RPC.call('test2').then(function (data) { console.log(data); }).done();
</script>

Example

Example running there demo.

About

wsrpc is WebSocket Remote procedure call for tornado and javascript

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 63.7%
  • Python 32.7%
  • HTML 3.4%
  • Shell 0.2%