-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathchat_client.py
More file actions
39 lines (28 loc) · 1.28 KB
/
chat_client.py
File metadata and controls
39 lines (28 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import asyncio
import logging
from typing import Optional
from rsocket.extensions.helpers import composite, route
from rsocket.extensions.mimetypes import WellKnownMimeTypes
from rsocket.frame_helpers import ensure_bytes
from rsocket.helpers import single_transport_provider, utf8_decode
from rsocket.payload import Payload
from rsocket.rsocket_client import RSocketClient
from rsocket.transports.tcp import TransportTCP
class ChatClient:
def __init__(self, rsocket: RSocketClient):
self._rsocket = rsocket
self._username: Optional[str] = None
async def login(self, username: str):
self._username = username
payload = Payload(ensure_bytes(username), composite(route('login')))
response = await self._rsocket.request_response(payload)
logging.info(f'Login response: {utf8_decode(response.data)}')
async def main():
connection = await asyncio.open_connection('localhost', 6565)
async with RSocketClient(single_transport_provider(TransportTCP(*connection)),
metadata_encoding=WellKnownMimeTypes.MESSAGE_RSOCKET_COMPOSITE_METADATA) as client1:
user = ChatClient(client1)
await user.login('user1')
if __name__ == '__main__':
logging.basicConfig(level=logging.INFO)
asyncio.run(main())