跳转到主要内容

WSRPC是针对aiohttp的WebSocket RPC

项目描述

WSRPC aiohttp

Github Actions

Coveralls

Latest Version

python wheel

Python Versions

license

为aiohttp服务器提供的易于使用的最小化WebSocket远程过程调用库。

请查看在线演示文档中的示例。

功能

  • 从客户端调用服务器功能;
  • 从服务器调用客户端功能(例如通知客户端事件);
  • 异步连接协议:服务器或客户端都能调用多个功能,并在任何顺序下尽快收到每个响应。
  • 完全异步的服务器端功能;
  • 将客户端到服务器和服务器到客户端的任何异常进行传输;
  • 无需依赖的即用型前端库;
  • 基于线程的WebSocket处理程序,用于编写完全同步的后端代码(例如同步数据库驱动程序等);
  • 受保护的服务器端方法(客户端不能直接调用以下划线开头的函数);
  • 用于内省的信号

安装

通过pip安装

pip install wsrpc-aiohttp

您可能希望安装可选的 ujson库以加速消息序列化/反序列化

pip install ujson

Python模块自带客户端JavaScript库。但对于纯JavaScript应用程序,您可以使用npm安装独立JavaScript客户端库

npm install @wsrpc/client

使用方法

后端代码

import logging
from time import time

import aiohttp.web
from wsrpc_aiohttp import Route, STATIC_DIR, WebSocketRoute, decorators


log = logging.getLogger(__name__)


# This class can be called by client.
# Connection object will have this class instance after calling route-alias.
class TestRoute(Route):
    # This method will be executed when client calls route-alias
    # for the first time.
    def init(self, **kwargs):
        # Python __init__ must be return "self".
        # This method might return anything.
        return kwargs

    # This method named by camelCase because the client can call it.
    @decorators.proxy
    async def getEpoch(self):

        # You can execute functions on the client side
        await self.do_notify()

        return time()

    # This method calls function on the client side
    @decorators.proxy
    async def do_notify(self):
        awesome = 'Somebody executed test1.getEpoch method!'
        await self.socket.call('notify', result=awesome)


app = aiohttp.web.Application()
app.router.add_route("*", "/ws/", WebSocketAsync)  # Websocket route
app.router.add_static('/js', STATIC_DIR)  # WSRPC js library
app.router.add_static('/', ".")  # Your static files

# Stateful request
# This is the route alias TestRoute as "test1"
WebSocketAsync.add_route('test1', TestRoute)

# Stateless request
WebSocketAsync.add_route('test2', lambda *a, **kw: True)


if __name__ == '__main__':
    logging.basicConfig(level=logging.DEBUG)
    aiohttp.web.run_app(app, port=8000)

前端代码

<script type="text/javascript" src="/js/wsrpc.min.js"></script>
<script>
    var url = (window.location.protocol==="https):"?"wss://":"ws://") + window.location.host + '/ws/';
    RPC = new WSRPC(url, 8000);

    // Configure client API, that can be called from server
    RPC.addRoute('notify', function (data) {
        console.log('Server called client route "notify":', data);
        return data.result;
    });
    RPC.connect();

    // Call stateful route
    // After you call that route, server would execute 'notify' route on the
    // client, that is registered above.
    RPC.call('test1.getEpoch').then(function (data) {
        console.log('Result for calling server route "test1.getEpoch": ', data);
    }, function (error) {
        alert(error);
    });

    // Call stateless method
    RPC.call('test2').then(function (data) {
        console.log('Result for calling server route "test2"', data);
    });
</script>

构建

只需运行

```bash
poetry run nox
```

版本控制

本软件遵循语义版本控制

项目详情


发行历史 发行通知 | RSS订阅

下载文件

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

源代码分发

wsrpc_aiohttp-4.0.3.tar.gz (34.2 kB 查看哈希值)

上传时间 源代码

构建分发

wsrpc_aiohttp-4.0.3-py3-none-any.whl (40.5 kB 查看哈希值)

上传时间 Python 3

支持者