项目作者: wsrpc

项目描述 :
WebSocket RPC for aiohttp
高级语言: Python
项目地址: git://github.com/wsrpc/wsrpc-aiohttp.git
创建时间: 2017-08-07T17:05:12Z
项目社区:https://github.com/wsrpc/wsrpc-aiohttp

开源协议:Apache License 2.0

下载


WSRPC aiohttp

Github Actions

Coveralls

Latest Version

python wheel

Python Versions

license

Easy to use minimal WebSocket Remote Procedure Call library for aiohttp
servers.

See online demo and
documentation with examples.

Features

  • Call server functions from the client side;
  • Call client functions from the server (for example to notify clients
    about events);
  • Async connection protocol: both server or client are able to call
    several functions and get responses as soon as each response would
    be ready in any order.
  • Fully async server-side functions;
  • Transfer any exceptions from a client side to the server side and
    vise versa;
  • Ready-to-use frontend-library without dependencies;
  • Thread-based websocket handler for writing fully-synchronous backend
    code (for synchronous database drivers etc.)
  • Protected server-side methods (cliens are not able to call methods,
    starting with underline directly);
  • Signals for introspection

Installation

Install via pip:

  1. pip install wsrpc-aiohttp

You may want to install optional
ujson library to speedup message
serialization/deserialization:

  1. pip install ujson

Python module provides client js library out of the box. But for pure
javascript applications you can install @wsrpc/client">standalone js client
library using npm:

  1. npm install @wsrpc/client

Usage

Backend code:

  1. import logging
  2. from time import time
  3. import aiohttp.web
  4. from wsrpc_aiohttp import Route, STATIC_DIR, WebSocketRoute, decorators
  5. log = logging.getLogger(__name__)
  6. # This class can be called by client.
  7. # Connection object will have this class instance after calling route-alias.
  8. class TestRoute(Route):
  9. # This method will be executed when client calls route-alias
  10. # for the first time.
  11. def init(self, **kwargs):
  12. # Python __init__ must be return "self".
  13. # This method might return anything.
  14. return kwargs
  15. # This method named by camelCase because the client can call it.
  16. @decorators.proxy
  17. async def getEpoch(self):
  18. # You can execute functions on the client side
  19. await self.do_notify()
  20. return time()
  21. # This method calls function on the client side
  22. @decorators.proxy
  23. async def do_notify(self):
  24. awesome = 'Somebody executed test1.getEpoch method!'
  25. await self.socket.call('notify', result=awesome)
  26. app = aiohttp.web.Application()
  27. app.router.add_route("*", "/ws/", WebSocketAsync) # Websocket route
  28. app.router.add_static('/js', STATIC_DIR) # WSRPC js library
  29. app.router.add_static('/', ".") # Your static files
  30. # Stateful request
  31. # This is the route alias TestRoute as "test1"
  32. WebSocketAsync.add_route('test1', TestRoute)
  33. # Stateless request
  34. WebSocketAsync.add_route('test2', lambda *a, **kw: True)
  35. if __name__ == '__main__':
  36. logging.basicConfig(level=logging.DEBUG)
  37. aiohttp.web.run_app(app, port=8000)

Frontend code:

  1. <script type="text/javascript" src="/js/wsrpc.min.js"></script>
  2. <script>
  3. var url = (window.location.protocol==="https):"?"wss://":"ws://") + window.location.host + '/ws/';
  4. RPC = new WSRPC(url, 8000);
  5. // Configure client API, that can be called from server
  6. RPC.addRoute('notify', function (data) {
  7. console.log('Server called client route "notify":', data);
  8. return data.result;
  9. });
  10. RPC.connect();
  11. // Call stateful route
  12. // After you call that route, server would execute 'notify' route on the
  13. // client, that is registered above.
  14. RPC.call('test1.getEpoch').then(function (data) {
  15. console.log('Result for calling server route "test1.getEpoch": ', data);
  16. }, function (error) {
  17. alert(error);
  18. });
  19. // Call stateless method
  20. RPC.call('test2').then(function (data) {
  21. console.log('Result for calling server route "test2"', data);
  22. });
  23. </script>

Build

Just run

  1. ```bash
  2. poetry run nox
  3. ```

Versioning

This software follows Semantic Versioning