I tried to use aiohttp as a websocket client. I need to implement a ping/pong mechanism to keep my client from disconnecting… I tried the following code (as a test):
import asyncio, uvloop, aiohttp async def main(): async with aiohttp.ClientSession() as session: async with session.ws_connect("ws://localhost:8080/ws") as ws: async for msg in ws: print(msg.type) if msg.type == aiohttp.WSMsgType.PING: print("PING Received") elif msg.type == aiohttp.WSMsgType.PONG: print("PONG Received") else: print(msg.data) if __name__ == "__main__": uvloop.install() loop = asyncio.get_event_loop() loop.run_until_complete(main())
But for some strange reason when I try to connect to a server that sends Pings/Pongs periodically I don’t receive them (meaning I don’t get a message printed) or they don’t get noticed ???
Is there something i am missing or did I do something wrong ?
If u need more information about the problem please ask.
Thanks to everyone who responds