mirror of
https://github.com/bigchaindb/bigchaindb.git
synced 2024-10-13 13:34:05 +00:00
Handle WS CLOSE properly
This commit is contained in:
parent
2afdd86752
commit
c4e0a8e1db
@ -70,6 +70,15 @@ class Dispatcher:
|
|||||||
|
|
||||||
self.subscribers[uuid] = websocket
|
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
|
@asyncio.coroutine
|
||||||
def publish(self):
|
def publish(self):
|
||||||
"""Publish new events to the subscribers."""
|
"""Publish new events to the subscribers."""
|
||||||
@ -115,11 +124,16 @@ def websocket_handler(request):
|
|||||||
msg = yield from websocket.receive()
|
msg = yield from websocket.receive()
|
||||||
except RuntimeError as e:
|
except RuntimeError as e:
|
||||||
logger.debug('Websocket exception: %s', str(e))
|
logger.debug('Websocket exception: %s', str(e))
|
||||||
return websocket
|
break
|
||||||
|
if msg.type == aiohttp.WSMsgType.CLOSED:
|
||||||
if msg.type == aiohttp.WSMsgType.ERROR:
|
logger.debug('Websocket closed')
|
||||||
|
break
|
||||||
|
elif msg.type == aiohttp.WSMsgType.ERROR:
|
||||||
logger.debug('Websocket exception: %s', websocket.exception())
|
logger.debug('Websocket exception: %s', websocket.exception())
|
||||||
return websocket
|
break
|
||||||
|
|
||||||
|
request.app['dispatcher'].unsubscribe(uuid)
|
||||||
|
return websocket
|
||||||
|
|
||||||
|
|
||||||
def init_app(event_source, *, loop=None):
|
def init_app(event_source, *, loop=None):
|
||||||
@ -157,3 +171,15 @@ def start(sync_event_source, loop=None):
|
|||||||
aiohttp.web.run_app(app,
|
aiohttp.web.run_app(app,
|
||||||
host=config['wsserver']['host'],
|
host=config['wsserver']['host'],
|
||||||
port=config['wsserver']['port'])
|
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)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user