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 API以编程方式创建ARM模板   java是否有合适的模式或方法来遍历数据和动态构造对象?   oop((类名)InstanceName)在java中有什么用途?   接口Java类方法未初始化   Java类:静态字段在内存中的位置?   java错误:服务类没有零参数构造函数   使用JDBC模板的java参数化sql查询   安卓致命异常:主java。lang.RuntimeException:   amazon web服务使用IAM角色从java访问AWS S3   java如何在Gradle中使用ojdbc   带块的Java类实例化   java日志记录是否有很大的开销,即使它是关闭的?   java JetBrains@Contract using字段   BluetoothGattCallback中从未调用java onDescriptorWrite()   java HttpMessageConverter无效的EOF处理   java在安卓的主活动类中调用另一个类   java防止在spring hibernate中存储重复值的正确方法   java如何在JAVADOC注释中使用变量?