1
我試圖使用Django渠道創建停留大家連接到插座持久的對象/是Django的渠道WebSocketConsumers無國籍
當我試圖創建多個運行之間保持持久的對象,它拋出一個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
我已經接受了,但是我會給這個+1,如果你詳細說明如何用'channel_session'來做這個事情,而不是請。 – cjds
@cjds channel_session用於存儲單個連接的數據,因此您無法使用它將數據公開給每個人。考慮將其存儲在像Redis這樣的東西中。 –