wsrpc websocket prc是龙卷风
aiohttp-wsrpc的Python项目详细描述
在浏览器和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。