跳转到主要内容

WSRPC WebSocket RPC for tornado

项目描述

Travis CI Latest Version https://img.shields.io/pypi/wheel/wsrpc-tornado.svg https://img.shields.io/pypi/pyversions/wsrpc-tornado.svg https://img.shields.io/pypi/l/wsrpc-tornado.svg

浏览器和tornado之间通过WebSocket进行远程过程调用。

特性

  • 从服务器端调用客户端函数。

  • 从客户端调用服务器方法。

  • 将任何异常从客户端传输到服务器端,反之亦然。

  • 前端库已经做得很好,无需任何修改即可使用。

  • 异步服务器端函数。

  • 基于线程的WebSocket处理程序,用于编写完全同步的代码(用于同步数据库驱动程序等)

  • 受保护的服务器端方法(以下划线开头,客户端端永远不会直接调用)

  • 异步连接协议。服务器或客户端可以调用多个方法,答案的顺序不可预测。

安装

通过pip安装

pip install wsrpc-tornado

如果您想,可以安装ujson

pip install ujson

简单用法

添加后端

from time import time
## If you want write async tornado code import it
# from from wsrpc import WebSocketRoute, WebSocket, wsrpc_static
## else you should use thread-base handler
from wsrpc import WebSocketRoute, WebSocketThreaded as WebSocket, wsrpc_static

tornado.web.Application((
    # js static files will available as "/js/wsrpc.min.js".
    wsrpc_static(r'/js/(.*)'),
    # WebSocket handler. Client will connect here.
    (r"/ws/", WebSocket),
    # Serve other static files
    (r'/(.*)', tornado.web.StaticFileHandler, {
         'path': os.path.join(project_root, 'static'),
         'default_filename': 'index.html'
    }),
))

# This class should be call by client.
# Connection object will be have the instance of this class when will call route-alias.
class TestRoute(WebSocketRoute):
    # This method will be executed when client will call route-alias first time.
    def init(self, **kwargs):
        # the python __init__ must be return "self". This method might return anything.
        return kwargs

    def getEpoch(self):
        # this method named by camelCase because the client can call it.
        return time()

# stateful request
# this is the route alias TestRoute as "test1"
WebSocket.ROUTES['test1'] = TestRoute

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

# initialize ThreadPool. Needed when using WebSocketThreaded.
WebSocket.init_pool()

添加前端

<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>

从服务器到客户端的逆向调用

后端

def do_notify(self):
    awesome = 'Notification for you!'
    yield self.socket.call('notify', result=awesome)

前端

<script>
    var url = (window.location.protocol==="https:"?"wss://":"ws://") + window.location.host + '/ws/';
    RPC = WSRPC(url, 5000);
    RPC.addRoute('notify', function (data) { return data.result; });
    RPC.connect();
</script>

示例

示例运行在此处 demo

项目详情


下载文件

下载您平台的文件。如果您不确定该选择哪个,请了解更多关于安装包的信息。

源分发

wsrpc-tornado-0.5.6.tar.gz (31.9 kB 查看哈希值)

上传时间

由支持