async def start_client(url):
session = ClientSession()
ws = await session.ws_connect(url)
return ws
async def write(ws):
while True:
await asyncio.sleep(1)
ws.send_str(host_name)
async def read(ws):
while True:
msg = await ws.receive()
print(msg.data)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
w_s = loop.run_until_complete(start_client('http://192.168.1.15/farm'))
tasks = [read(w_s), write(w_s)]
loop.run_until_complete(asyncio.gather(*tasks))
# loop.run_forever()
--
You received this message because you are subscribed to the Google Groups "aio-libs" group.
To unsubscribe from this group and stop receiving emails from it, send an email to aio-libs+u...@googlegroups.com.
To post to this group, send email to aio-...@googlegroups.com.
Visit this group at https://groups.google.com/group/aio-libs.
To view this discussion on the web visit https://groups.google.com/d/msgid/aio-libs/b36927af-6142-4b04-9823-61189d7b5c2a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
1. Почему не работает?
2. Что вы сделал чтобы создавать новое ws соединение и отправлять данные периодически.
P.S. На stackoverflow такие вопросы банят.
ClientWebSocketResponse, передал указатель в корутины для чтения и записи сокета.async def start_client(loop, url):
session = ClientSession()
ws = await session.ws_connect(url)
loop.create_task(write(ws))
while True:
msg = await ws.receive()
print(msg.data)
async def write(ws):
while True:
await asyncio.sleep(1)
ws.send_str(host_name)
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(start_client(loop, 'http://192.168.1.15/farm'))
async def start_client(loop, url):
session = ClientSession()
was_connect = False
try:
ws = await session.ws_connect(url)
was_connect = True
print('Connected to server:', host_ip + ':' + str(host_port))
except:
pass
else:
send_task = loop.create_task(send_to_server(ws))
async for msg in ws:
if msg.type == WSMsgType.TEXT:
pass
elif msg.type == WSMsgType.CLOSE:
break
elif msg.type == WSMsgType.CLOSED:
break
elif msg.type == WSMsgType.ERROR:
break
send_task.cancel()
finally:
session.close()
if was_connect:
print('Disconnected...')
return
async def send_to_server(ws):
while True:
await asyncio.sleep(1)
ws.send_str(host_name)
async def main(loop):
url = 'ws://192.168.1.15:30080/farm'
while True:
await asyncio.sleep(1)
print('Connecting to server...')
session_task = loop.create_task(start_client(loop, url))
await session_task
if __name__ == '__main__':
loop = asyncio.get_event_loop()
loop.run_until_complete(main(loop))
loop.close()