Handle WS CLOSE properly

This commit is contained in:
vrde 2017-10-19 12:19:43 +02:00
parent 2afdd86752
commit c4e0a8e1db
No known key found for this signature in database
GPG Key ID: 6581C7C39B3D397D

View File

@ -70,6 +70,15 @@ class Dispatcher:
self.subscribers[uuid] = websocket
def unsubscribe(self, uuid):
"""Remove a websocket from the list of subscribers.
Args:
uuid (str): a unique identifier for the websocket.
"""
del self.subscribers[uuid]
@asyncio.coroutine
def publish(self):
"""Publish new events to the subscribers."""
@ -115,10 +124,15 @@ def websocket_handler(request):
msg = yield from websocket.receive()
except RuntimeError as e:
logger.debug('Websocket exception: %s', str(e))
return websocket
if msg.type == aiohttp.WSMsgType.ERROR:
break
if msg.type == aiohttp.WSMsgType.CLOSED:
logger.debug('Websocket closed')
break
elif msg.type == aiohttp.WSMsgType.ERROR:
logger.debug('Websocket exception: %s', websocket.exception())
break
request.app['dispatcher'].unsubscribe(uuid)
return websocket
@ -157,3 +171,15 @@ def start(sync_event_source, loop=None):
aiohttp.web.run_app(app,
host=config['wsserver']['host'],
port=config['wsserver']['port'])
if __name__ == '__main__':
def meow(queue):
while True:
yield from asyncio.sleep(1)
yield from queue.put('meow')
loop = asyncio.get_event_loop()
event_source = asyncio.Queue(loop=loop)
# loop.create_task(meow(event_source))
app = init_app(event_source, loop=loop)
aiohttp.web.run_app(app)