我正在使用channel == 1.1.7和asgi-redis == 1.4.3。我想使用以下命令获取当前会话的用户:
user = get_redis_instance().get(id)
具有定义如下的get_redis_instance函数:
def get_redis_instance():
from myproject.asgi import channel_layer
return channel_layer._connection_list[0]
但是它什么也没有返回!
上面的代码在使用者内部:
class MyprojectWebsocket(WebsocketConsumer, WebsocketAuth):
http_user = True
channel_session = True
channel_session_user = True
def authenticate(self):
if user.is_authenticated
if get_redis_instance().get(id) != None:
return True
else:
return False
def connect(self, message, id):
self.message.reply_channel.send({"accept": True})
user = get_redis_instance().get(id)
print("user: {}".format(user))
if not self.authenticate(id):
self.message.reply_channel.send({"text": json.dumps(
{'status': False, 'message': 'You must login to the system!'})}, immediately=True)
self.message.reply_channel.send({"accept": False})
id为当前用户的ID