从使用者的connect内部发送websockets消息会关闭连接

问题描述

在跟踪this question的根本原因时,我遇到了一个问题,即在self.send()的{​​{1}}内部使用connect()。它关闭websocket而不发送消息:

AsyncWebsocketConsumer

这是否是预期的行为,我不应该尝试通过WebSocket从Consumer的import json from uuid import UUID from channels.generic.websocket import AsyncWebsocketConsumer from myapp.models import AnalysisResult class AnalysisConsumer(AsyncWebsocketConsumer): def __init__(self,*args,**kwargs): super().__init__(*args,**kwargs) self.analysis_id = self.scope['url_route']['kwargs']['analysis_id'] self.analysis_group_name = "analysis_{}".format(self.analysis_id) async def connect(self): await self.channel_layer.group_add(self.analysis_group_name,self.channel_name) # Setting up this whole channel / websocket connection takes a while. Thus,we need # to send an initial update to the client to make sure a (too) fast analysis result # doesn't get lost while we've been busy here. # Problem: This is disCONNECTING instead of sending the message. await self.send(text_data=json.dumps({ 'progress_percent': 100,'status_text': "Done already." })) await self.accept() async def disconnect(self,code): await self.channel_layer.group_discard(self.analysis_group_name,self.channel_name) 方法内部发送内容吗?还是应该起作用?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)