我正在编写一个需要使用套接字的应用程序。为了处理它,我使用django频道,但我不能向组广播消息。
class waiting(WebsocketConsumer):
def connect(self):
self.channel_layer.group_add( 'my_group_name', self.channel_name )
if <condition>:
self.channel_layer.group_send( # This does not give any effect. I'm sure the condition is satisfied.
'my_group_name',
{
'message' : 'succes'
}
)
self.accept()
self.send(json.dumps({'message' : 'message'})) # This works perfect
def disconnect(self, close_code):
self.channel_layer.group_discard(
'my_group_name',
self.channel_name
)我哪里错了?我已经阅读了很多教程和文档,但我没有找到任何解决方案。我应该怎么做才能让这段代码像我想要的那样工作?
我使用的是通道== 2.3.1和Django 2.2.6。
发布于 2019-12-09 12:20:31
在广播之前,您实际上并没有接受connect函数中的连接。如果没有self.accept(),连接将被拒绝并关闭。
https://stackoverflow.com/questions/59225621
复制相似问题