MCPcopy Create free account
hub / github.com/actix/examples / start_client

Function start_client

websockets/echo/websocket-client.py:9–46  ·  view source on GitHub ↗
(loop, url)

Source from the content-addressed store, hash-verified

7import aiohttp
8
9async def start_client(loop, url):
10 name = input('Please enter your name: ')
11
12 # send request
13 ws = await aiohttp.ClientSession().ws_connect(url, autoclose=False, autoping=False)
14
15 # input reader
16 def stdin_callback():
17 line = sys.stdin.buffer.readline().decode('utf-8')
18 if not line:
19 loop.stop()
20 else:
21 ws.send_str(name + ': ' + line)
22 loop.add_reader(sys.stdin.fileno(), stdin_callback)
23
24 async def dispatch():
25 while True:
26 msg = await ws.receive()
27
28 if msg.type == aiohttp.WSMsgType.TEXT:
29 print('Text: ', msg.data.strip())
30 elif msg.type == aiohttp.WSMsgType.BINARY:
31 print('Binary: ', msg.data)
32 elif msg.type == aiohttp.WSMsgType.PING:
33 await ws.pong()
34 elif msg.type == aiohttp.WSMsgType.PONG:
35 print('Pong received')
36 else:
37 if msg.type == aiohttp.WSMsgType.CLOSE:
38 await ws.close()
39 elif msg.type == aiohttp.WSMsgType.ERROR:
40 print('Error during receive %s' % ws.exception())
41 elif msg.type == aiohttp.WSMsgType.CLOSED:
42 pass
43
44 break
45
46 await dispatch()
47
48
49ARGS = argparse.ArgumentParser(

Callers 1

Calls 1

dispatchFunction · 0.70

Tested by

no test coverage detected