-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.py
More file actions
34 lines (29 loc) · 957 Bytes
/
client.py
File metadata and controls
34 lines (29 loc) · 957 Bytes
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
import websockets
import asyncio
import avro.schema
import io
import avro.io
async def recv_avro():
test_schema = '''
{
"namespace": "example.avro",
"type": "record",
"name": "User",
"fields": [
{"name": "name", "type": "string"},
{"name": "favorite_number", "type": ["int", "null"]},
{"name": "favorite_color", "type": ["string", "null"]}
]
}
'''
schema = avro.schema.Parse(test_schema)
async with websockets.connect('ws://localhost:5670') as websocket:
while True:
raw_bytes = await websocket.recv()
bytes_reader = io.BytesIO(raw_bytes)
decoder = avro.io.BinaryDecoder(bytes_reader)
reader = avro.io.DatumReader(schema)
user = reader.read(decoder)
print("Received...")
print(user)
asyncio.get_event_loop().run_until_complete(recv_avro())