I'm trying to use Django channels to create an object that stays persistent for everyone connected to the socket/
When I try to create an object that stays persistent between multiple receive()
runs, it throws a NoneType
exception
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
Class-based consumers are uninstantiated, i. e. each time a new message gets routed to a consumer, it creates an entirely new consumer. Hence there is no way to persist data between to messages in the consumer itself.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With