如何解决从使用者的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 举报,一经查实,本站将立刻删除。