wsrpc websocket prc是龙卷风

aiohttp-wsrpc的Python项目详细描述


https://travis-ci.org/mosquito/wsrpc.svg

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

功能

  • 正在从服务器端启动调用客户端功能。
  • 从客户端调用服务器方法。
  • 将任何异常从客户端传输到服务器端,反之亦然。
  • 前端库的使用做得很好,没有任何修改。
  • 完全异步服务器端功能。
  • 基于线程的WebSocket处理程序,用于编写完全同步的代码(用于同步数据库驱动程序等)
  • 受保护的服务器端方法(以下划线开头的永远不会直接从客户端调用)
  • 异步连接协议。服务器或客户端可以调用多个具有不可预知的答案顺序的方法。

安装

通过PIP安装:

pip install wsrpc-tornado

如果需要,请安装ujson:

pip install ujson

简单用法

添加后端

fromtimeimporttime## If you want write async tornado code import it# from from wsrpc import WebSocketRoute, WebSocket, wsrpc_static## else you should use thread-base handlerfromwsrpcimportWebSocketRoute,WebSocketThreadedasWebSocket,wsrpc_statictornado.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.classTestRoute(WebSocketRoute):# This method will be executed when client will call route-alias first time.definit(self,**kwargs):# the python __init__ must be return "self". This method might return anything.returnkwargsdefgetEpoch(self):# this method named by camelCase because the client can call it.returntime()# stateful request# this is the route alias TestRoute as "test1"WebSocket.ROUTES['test1']=TestRoute# stateless requestWebSocket.ROUTES['test2']=lambda*a,**kw:True# initialize ThreadPool. Needed when using WebSocketThreaded.WebSocket.init_pool()

添加前端侧

<scripttype="text/javascript"src="/js/q.min.js"></script><scripttype="text/javascript"src="/js/wsrpc.min.js"></script><script>varurl=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>

示例

在那里运行的示例demo

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何将springboot war部署到debian jetty8   java破坏了函数,即使它看起来是正确的   当MockMvc不可自动编译时,java会有选择地从Spring Security向其应用单个过滤器   java AndroidStudio每秒播放n次生成的声音   JavaJBossSeam:继承的方法被拦截了吗?   ScheduledExecutorService中线程默认名称中池号的java含义   在Java中用递归方法实现类Fibonacci的递归   java虚拟路径/文件夹   java即时与ZoneDateTime。转换到另一时区   用GaussLegendre算法在java中逼近Pi   java RecyclerView findFirstVisibleItemPosition()返回1   java根据当前经过身份验证的用户的角色返回不同的JSON对象   java从内部类访问扩展类的方法   Android程序中的Java语法WRT静态方法   可选参数初始化Java字符串文字时出错