根據安德魯·戈德溫(頻道包的開發者),當你有一個被斷開的通道此消息被記錄,但不能從信道組已移除:
是啊,這是達芙妮比以前更加冗長,我需要刪除它。不要擔心 - 斷開仍然在一個組中的頻道後,這很正常。不過,您可能希望在斷開連接處理程序中添加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
...