Skip to content

Instantly share code, notes, and snippets.

@codeif
Created February 12, 2019 18:44
Show Gist options
  • Save codeif/5a0b69335a1482e692801e51b8fc5950 to your computer and use it in GitHub Desktop.
Save codeif/5a0b69335a1482e692801e51b8fc5950 to your computer and use it in GitHub Desktop.
import aiohttp
import asyncio
async def main():
session = aiohttp.ClientSession()
ws = await session.ws_connect(
'wss://ws.example.com/channel')
while True:
msg = await ws.receive()
if msg.type == aiohttp.WSMsgType.TEXT:
if msg.data == 'close':
await ws.close()
break
else:
print(msg.data)
# ws.send_str(msg.data + '/answer')
elif msg.tp == aiohttp.MsgType.closed:
break
elif msg.tp == aiohttp.MsgType.error:
break
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment