Websockets extended with RPC.
Project description
RG Websocket exended with RPC
Simple interface for writing Websocket application with RPC.
Installation
PIP official
pip install rgws
Dev
pip install git+https://github.com/Redict/rg_websocket.git
Usage:
Server
class SimpleServerInterface(WebsocketServer):
def __init__(self, **kwargs):
super(SimpleServerInterface, self).__init__(**kwargs)
self._register(self.example_func)
"""
This overrides _consumer method in WebsocketServer, there should
business logic be placed if any. At this point we are just
dispatching function from message and sending result back.
"""
async def _consumer(self, websocket, message):
ret = await self.dispatch(message)
async for gen in ret:
await websocket.send(gen)
async def example_func(self, bla):
yield json.dumps({"resp": bla})
async def stream_func(self):
data = [0] * 2 ** 32
return self.make_data_stream(data)
Client
class SimpleClientInterface(WebsocketClient):
def __init__(self, **kwargs):
super(SimpleClientInterface, self).__init__(**kwargs)
"""
This is business logic for client, basically in this example
we just connects to server and trying to call `example_func` once
then exits.
"""
async def _producer(self, websocket):
logging.debug(await self.example_func("blo"))
# if you want to pass function with arguments, you can use functools.partial(func, args)
logging.debug(await self.read_data_stream(websocket, self.stream_func))
You can make request with other way:
c = SimpleClientInterface(host=..., port=...)
async def main():
while True:
if not c:
continue
logging.debug(c.example_func())
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(asyncio.gather(c.run(), main()))
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
rgws-0.1.4.tar.gz
(5.2 kB
view hashes)