django websockets无法在频道上发送消息
问题描述:
我在Django上使用Redis上的网络套接字。 Django的运行MacOS的服务器上很好,但我一开始红帽Linux服务器上运行它,现在的服务器给了我这个错误,每当我过的WebSockets发送一个包:django websockets无法在频道上发送消息
ERROR - server - HTTP/WS send decode error:
Cannot dispatch message on channel
u'daphne.response.fzdRCEVZkh!nqhIpaLfWb' (unknown)
注意:当出现错误时,包会被正确接收。
我找不到此错误的任何资源。
我跟着official instructions寻找频道。
答
根据安德鲁·戈德温(频道包的开发者),当你有一个被断开的通道此消息被记录,但不能从信道组已移除:
是啊,这是达芙妮比以前更加冗长,我需要删除它。不要担心 - 断开仍然在一个组中的频道后,这很正常。不过,您可能希望在断开连接处理程序中添加Group.discard调用来停止它。
我有同样的错误,使用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
...