使用框架:django + channels
我写了一个http请求async_receive_msg
调用如下async_receive_msg方法,但当前通道名已断开连接,
虽然方法调用成功,但是实际上消息是不能发送出去的,导致消息在通道中堆积,我该如何操作才能删除这些没用的消息?
或者如何校验当前通道是否仍在连接,如何丢弃这些消息?
@csrf_exempt
@async_to_sync
async def async_receive_msg(request):
"""
调用当前方法
"""
try:
room = data.get('room')
channel_name = await get_channel_name_by_code(room)
if not channel_name:
logger.info('推送点赞信息给房间{}时检测到当前房间未连接'.format(room))
return JsonResponse({'result': 1, 'msg': 'room{} is not connected!'.format(room)})
await channel_layer.send(channel_name, {
"type": "group_message",
"message": "msg",
})
return JsonResponse({'result': 1, 'msg': 'success'})
except Exception as e:
logger.error(e)
return JsonResponse({'result': 2, 'errorMsg': str(e)})
class AsyncConsumer(AsyncWebsocketConsumer):
async def connect(self):
await self.accept()
async def disconnect(self, close_code=None):
...
async def group_message(self, event):
"""
消息推送
"""
await self.send(text_data=json.dumps({
'result': 1,
"message": event.get("message")
}))