Skip to main content

Websockets extended with RPC.

Reason this release was yanked:

bytes to JSON fragmentation in 0.1.4

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


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.3.tar.gz (5.1 kB view hashes)

Uploaded Source

Built Distribution

rgws-0.1.3-py2.py3-none-any.whl (4.0 kB view hashes)

Uploaded Python 2 Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page