本文整理汇总了Python中aiohttp.WSMsgType.CLOSED属性的典型用法代码示例。如果您正苦于以下问题:Python WSMsgType.CLOSED属性的具体用法?Python WSMsgType.CLOSED怎么用?Python WSMsgType.CLOSED使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类aiohttp.WSMsgType
的用法示例。
在下文中一共展示了WSMsgType.CLOSED属性的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: websocket_msg_handler
# 需要导入模块: from aiohttp import WSMsgType [as 别名]
# 或者: from aiohttp.WSMsgType import CLOSED [as 别名]
def websocket_msg_handler(self):
"""Handle active websocket connection, returning upon close"""
async for msg in self.websocket:
LOGGER.debug("Websocket message: %s, %s", msg.type, msg.data)
if msg.type in (WSMsgType.CLOSED, WSMsgType.ERROR):
return None
if msg.data == 'refresh-cache':
await self._refresh_cache()
msg = f"refreshed {BaseHandler.db_cache.dbchange['exported']}"
if self.websocket:
await self.websocket.send_str(msg)
else:
self.websocket_response_queue.add(msg)
else:
LOGGER.warning("Unhandled websocket message %s", msg.data)
示例2: receive
# 需要导入模块: from aiohttp import WSMsgType [as 别名]
# 或者: from aiohttp.WSMsgType import CLOSED [as 别名]
def receive(self):
msg = await self.ws.receive()
if msg.type == WSMsgType.TEXT:
return msg.data
elif msg.type == WSMsgType.ERROR:
raise ConnectionClosedException()
elif msg.type == WSMsgType.CLOSING:
raise ConnectionClosedException()
elif msg.type == WSMsgType.CLOSED:
raise ConnectionClosedException()
示例3: test_receive_closed
# 需要导入模块: from aiohttp import WSMsgType [as 别名]
# 或者: from aiohttp.WSMsgType import CLOSED [as 别名]
def test_receive_closed(self, mock_ws):
mock_ws.receive.return_value.type = WSMsgType.CLOSED
connection_context = AiohttpConnectionContext(ws=mock_ws)
with pytest.raises(ConnectionClosedException):
await connection_context.receive()
示例4: _ws_handler
# 需要导入模块: from aiohttp import WSMsgType [as 别名]
# 或者: from aiohttp.WSMsgType import CLOSED [as 别名]
def _ws_handler(self, request, group):
"""
Handle websocket connections.
This includes:
* new connections
* closed connections
* messages
"""
ws_client = web.WebSocketResponse()
await ws_client.prepare(request)
session = None
if self.websockets[group]["authenticated"]:
session = await get_session(request)
if session.new:
self.logger.debug('websocket: not logged in')
if asyncio.iscoroutinefunction(ws_client.send_str):
await ws_client.send_str(json.dumps({'status': 401, 'text': "Unauthorized"}))
else:
ws_client.send_str(json.dumps({'status': 401, 'text': "Unauthorized"}))
ws_client.close()
return ws_client
ws_client.cirrina = CirrinaWSContext(request, session)
self.websockets[group]["connections"].append(ws_client)
try:
await self.websockets[group]["connect"](ws_client)
except Exception as exc:
self.logger.error("websocket: error in connect event handler")
self.logger.exception(exc)
while True:
try:
msg = await ws_client.receive()
if msg.type == WSMsgType.CLOSE or msg.type == WSMsgType.CLOSED:
self.logger.info('websocket closed')
break
self.logger.debug("websocket got: %s", msg)
if msg.type == WSMsgType.TEXT:
await self.websockets[group]["handler"](ws_client, msg.data)
elif msg.type == WSMsgType.ERROR:
self.logger.info('websocket closed with exception %s', ws_client.exception())
except futures._base.CancelledError:
pass
except Exception as exc:
self.logger.exception(exc)
self.websockets[group]["connections"].remove(ws_client)
try:
await self.websockets[group]["disconnect"](ws_client)
except Exception as exc:
self.logger.error("websocket: error in disconnect event handler")
self.logger.exception(exc)
return ws_client
# JRPC protocol