Support RSocket
Skip to main content

Server - TCP Transport

rsocket-py supports multiple transport protocols. The TCP transport is available by default. In order to use a basic TCP transport, instantiate a TransportTCP and pass it to an RSocketServer instance.

Example

import asyncio
import logging
import sys

from rsocket.payload import Payload
from rsocket.request_handler import BaseRequestHandler
from rsocket.rsocket_server import RSocketServer
from rsocket.transports.tcp import TransportTCP


class Handler(BaseRequestHandler):
async def request_response(self, payload: Payload) -> asyncio.Future:
...


async def run_server(server_port):
logging.info('Starting server at localhost:%s', server_port)

def session(*connection):
RSocketServer(TransportTCP(*connection), handler_factory=Handler)

server = await asyncio.start_server(session, 'localhost', server_port)

async with server:
await server.serve_forever()


if __name__ == '__main__':
port = sys.argv[1] if len(sys.argv) > 1 else 6565
logging.basicConfig(level=logging.DEBUG)
asyncio.run(run_server(port))