根据安德鲁·戈德温(频道包的开发者),当你有一个被断开的通道此消息被记录,但不能从信道组已移除:
是啊,这是达芙妮比以前更加冗长,我需要删除它。不要担心 - 断开仍然在一个组中的频道后,这很正常。不过,您可能希望在断开连接处理程序中添加Group.discard调用来停止它。
Source.
我有同样的错误,使用channels.generic.websockets.WebsocketConsumer
定制IMPL。清除disconnect
回叫中组的频道后,该消息消失。
基于类的使用者的一个简短示例:假设您将客户端添加到名为foo
的广播组,并建立连接。然后,在客户端断开连接时,从组中删除其通道:
from channels import Group
from channels.generic.websockets import JsonWebsocketConsumer
class MyConsumer(JsonWebsocketConsumer):
groupname = 'foo'
def connect(self, message, **kwargs):
# send an accept or the connection will be dropped automatically
self.message.reply_channel.send({"accept": True})
# add the channel to the broadcast group
Group(self.groupname).add(message.reply_channel)
# do the rest of logic that should happen on connection established
...
def disconnect(self, message, **kwargs):
Group(self.groupname).discard(message.reply_channel)
# do the rest of logic that should happen on disconnect
...