Skip to content

Instantly share code, notes, and snippets.

@barrachri
Last active August 31, 2017 09:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save barrachri/3385c5e7bb46fd9736588475e0e98ca8 to your computer and use it in GitHub Desktop.
Save barrachri/3385c5e7bb46fd9736588475e0e98ca8 to your computer and use it in GitHub Desktop.
import asyncio
import logging
import aiohttp
from aiohttp import web
from auth.utils import login_required
import aiodocker
from aiodocker.docker import Docker
from aiodocker.exceptions import DockerError
log = logging.getLogger(__name__)
@login_required
async def websocket_handler(request, user_id):
ws = web.WebSocketResponse()
await ws.prepare(request)
request.app['websockets'][user_id].add(ws)
task = request.app.loop.create_task(
listen_to_docker(ws))
try:
async for msg in ws:
# handle incoming messages
if msg.type == aiohttp.WSMsgType.close:
log.debug('websocket connection closed')
await ws.close()
break
elif msg.type == aiohttp.WSMsgType.error:
log.debug('ws connection closed with exception %s' % ws.exception())
break
elif msg.type == aiohttp.WSMsgType.text:
log.debug(msg)
await ws.send_str('ECHO')
else:
print('ws connection received unknown message type %s' % msg.type)
except asyncio.CancelledError:
log.debug('websocket cancelled')
finally:
request.app['websockets'][user_id].remove(ws)
await ws.close()
return ws
docker = Docker()
subscriber = docker.events.subscribe()
async def listen_to_docker(ws):
log.debug("Running Listening Docker")
try:
while True:
event = await subscriber.get()
log.debug(event)
# Forward message to all connected websockets:
if event['Type'] == 'container':
log.debug(ws)
await ws.send_str('Message from docker')
log.debug("Message sent")
except asyncio.CancelledError:
pass
finally:
await docker.close()
log.debug("Outside the loop!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment