def login_handler(self): username = self.jdata.get("username") password = self.jdata.get("password") if not all([username, password]): return {"code": "LOGIN_FAIL", "message": "You need send username and password"} else: try: if not r.exists("user:"******":password"): return {"code": "LOGIN_FAIL", "message": "Username is not existed"} if password == r.get("user:"******":password"): if self.is_recv_boardcast: recv_connections.append(self) else: connections.append(self) self.user = username self.isLogin = True if not self.is_recv_boardcast: self.broadcast_login() r.sadd("online_user", username) #self.broadcast({"action": "login", "user": self.get_user(), "nickname": r.get("user:"******":nickname")}) return {"code": "LOGIN_SUCCESS", "message": "success", "nickname": r.get("user:"******":nickname")} else: return {"code": "LOGIN_FAIL", "message": "Username and password not match"} except Exception, e: print "Error:", e.message,traceback.format_exc() return {"code": "SERVER_ERROR", "message": "Server Error"}
def is_unique(conversation, func): msg_id = func(conversation) if r.exists("msg:" + msg_id): return False else: r.set("msg:" + msg_id, True) return True
def register_handler(self): username = self.jdata.get("username") password = self.jdata.get("password") nickname = self.jdata.get("nickname") if not all([username, password, nickname]): return {"code": "REGISTER_FAIL", "message": "You need send username, password and nickname"} else: try: if r.exists("user:"******":password"): return {"code":"REGISTER_FAIL", "message": "Username is registered"} r.set("user:"******":password", password) r.set("user:"******":nickname", nickname) return {"code": "REGISTER_SUCCESS", "message": "success"} except Exception, e: print "Error:",e.message,traceback.format_exc() return {"code": "SERVER_ERROR", "message": "Server Error"}
def user_exists(user_id): return r.exists("save:" + user_id)