我正在尝试使用 Django 通道创建一个对象,该对象对于连接到套接字的每个人都保持持久性/
当我尝试创建一个在多次receive()
运行之间保持持久性的对象时,它会引发NoneType
异常
class MyConsumer(WebsocketConsumer):
def __init__(self,path):
self.protocol = None
WebsocketConsumer.__init__(self, path)
def connection_groups(self):
return ["test"]
# Connected to websocket.connect
def connect(self,message):
try:
self.protocol = "hello"
except Exception as exc:
print ("Unable to accept incoming connection. Reason: %s" % str(exc))
self.message.reply_channel.send({"accept": True})
# Connected to websocket.receive
def receive(self,text=None, bytes=None):
text = self.protocol[1] # This throws an error that says protocol is none
self.send(text=text, bytes=bytes)
# Connected to websocket.disconnect
def disconnect(self,message):
pass