Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
6.13
  • Loading branch information
suzxzd committed Jun 13, 2024
1 parent 40f5323 commit 92f445d
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 43 deletions.
Binary file modified ChatRoomPlan.emmx
Binary file not shown.
Binary file added main/__pycache__/mysql.cpython-310.pyc
Binary file not shown.
45 changes: 21 additions & 24 deletions main/client/Client.py
Expand Up @@ -46,17 +46,11 @@ def stop_loop(self):

@abstractmethod
def on_connect(self, client, userdata, flags, rc):
if rc == 0:
print("Connected successfully")
else:
print("Failed to connect, return code %d\n", rc)
pass

@abstractmethod
def on_message(self, client, userdata, msg):
# 规定传入数据均为dict的形式
data = eval(msg.payload.decode('utf-8'))
print(data)
return data
pass
def clientStart(self):#启动进程,使用threading(python自带进程管理库)进行管理
loopThread = threading.Thread(target=self.start_loop)
loopThread.start()
Expand Down Expand Up @@ -93,11 +87,6 @@ def on_message(self,client, userdata, msg):#接受数据
return data

def publishRegister(self):
T1 = time.time() # 记录发布时间
message = f"Hello World | {T1}"
topic = 'test/topic'
self.client.publish(topic, message,2)

returnTopic=self.userName+"register"
#数据发送特定格式
data = {'userName': self.userName, 'userPwd': self.userPwd, 'returnTopic': returnTopic}
Expand All @@ -110,14 +99,15 @@ class Login(Client):
def __init__(self,userName,userPwd):
super().__init__(userName,userPwd)
self.clientStart()
self.publishRegister()
self.publishLogin()



def on_connect(self, client, userdata, flags, rc):#链接
if rc == 0:
print("Connected successfully")
returnRegister=self.userName+"register"
client.subscribe(returnRegister) # 订阅 return 主题
returnLogin=self.userName+"login"
client.subscribe(returnLogin) # 订阅 return 主题
else:
print("Failed to connect, return code %d\n", rc)

Expand All @@ -128,22 +118,29 @@ def on_message(self,client, userdata, msg):#接受数据
#读取数据
code=data.get('code')
message=data.get('message')
if code==0:
if code==0: #表示登入失败
print(message)
if code==1:
if code==1: #表示登入成功
print(message)
return data

def publishLogin(self):
pass
returnTopic=self.userName+"login"
#数据发送特定格式
data = {'userName': self.userName, 'userPwd': self.userPwd, 'returnTopic': returnTopic}
# qos1
self.client.publish(publish_topic["login_topic"], str(data).encode(), 1)
# client.loop()
print('发布信息 ', publish_topic['login_topic'], ' 成功')


######################################
#tao yu
class Like(Client):
def __init__(self,userName,userPwd):
super().__init__(userName,userPwd)
def __init__(self,messageId):
super().__init__()
self.clientStart()
self.publishRegister()
self.publish_like(messageId)

@abstractmethod
def on_connect(self, client, userdata, flags, rc):
Expand All @@ -160,8 +157,8 @@ def publish_like(client, messageId):

####################################################################
def main():
r=Register("hhh","123456")
b=Register("jjj",'123')
#Register("hhhhh","123456")
Login("hhhhhh","123456")

main()

Expand Down
Binary file added main/config/__pycache__/config.cpython-310.pyc
Binary file not shown.
28 changes: 23 additions & 5 deletions main/mysql.py
Expand Up @@ -20,7 +20,7 @@ class Meta:
class UserModel(BaseModel):
id=AutoField()
name = CharField(max_length=64)
password = CharField(max_length=32)
password = CharField(max_length=128)


class Meta:
Expand All @@ -36,11 +36,21 @@ class ChatModel(BaseModel):
class Meta:
db_table = 'chat'

#点赞类
class LikeModel(BaseModel):
id = AutoField()
messageId = ForeignKeyField(ChatModel, backref='likes')
count = IntegerField(default=0)

class Meta:
db_table = 'like'

class CreateTable:
@staticmethod
def create():
db.connect()
db.create_tables([UserModel, ChatModel])
db.create_tables([UserModel, ChatModel,LikeModel])



#用户操作
Expand All @@ -53,7 +63,6 @@ def hash_password(password):
return hashed

# 验证密码

def check_password(hashed, password):
# 验证密码
return bcrypt.checkpw(password.encode('utf-8'), hashed)
Expand All @@ -73,7 +82,7 @@ def chackName(name):#检测用户是否存在,不存在则打印提示并返
print(f"An error occurred: {error}")
return None
@staticmethod
def chackPassword(password):#如果密码正确则
def chackPassword(password):#如果密码正确则返回True
# 密码加密
hashPassword = hash_password(password)
return check_password(hashPassword,password)
Expand All @@ -89,6 +98,13 @@ def addUser(name, password):
print(error)
return False

@staticmethod
def verifyUser(name, password):
user = UserManage.chackName(name)
if user is None:
return False
hashed_password_bytes = user.password.encode('utf-8')
return check_password(hashed_password_bytes, password)

################################################################
class ChatManage:
Expand Down Expand Up @@ -123,7 +139,6 @@ def AddChat(message,senderId):
return 'Fail'


CreateTable.create()

#################################################################
#taoyu:
Expand Down Expand Up @@ -174,3 +189,6 @@ def like_message(messageId):
except Exception as error:
print(f"An error occurred: {error}")
return None



24 changes: 10 additions & 14 deletions main/server/Server.py
Expand Up @@ -65,15 +65,12 @@ class Register(Server):
def __init__(self):
super().__init__()
self.serverStart()


def on_connect(self, client, userdata, flags, rc):
if rc == 0:
print("Connected successfully")
client.subscribe(publish_topic['register_topic']) # 订阅 login 主题
client.subscribe(publish_topic['register_topic']) # 订阅 register 主题
else:
print("Failed to connect, return code %d\n", rc)

def on_message(self, client, userdata, msg):
# 规定传入数据均为dict的形式
data = eval(msg.payload.decode('utf-8'))
Expand Down Expand Up @@ -128,19 +125,18 @@ def on_message(self, client, userdata, msg):
def login(self, userName, userPwd, returnTopic):
user = UserManage.chackName(userName)
if user == None:
print("用户不存在,允许注册")
if UserManage.addUser(userName, userPwd):
print("用户添加成功!")
data = {'code': 1, "message": "用户添加成功!"}
print("账号或密码错误")
data = {'code': 0, 'message':"账号或密码错误"}
self.client.publish(returnTopic, str(data).encode(), 1)
else:
if UserManage.verifyUser(userName,userPwd):
print(f"{userName}通过验证,欢迎")
data = {'code': 1, "message": f"验证通过,欢迎回来{userName}"}
self.client.publish(returnTopic, str(data).encode(), 1)
else:
print("用户添加失败,错误发生在服务器!")
data = {'code': 0, "message": "用户添加失败,错误发生在服务器!"}
print("账号或密码错误")
data = {'code': 0, 'message': "账号或密码错误"}
self.client.publish(returnTopic, str(data).encode(), 1)
else:
print("用户存在,不允许注册")
data = {'code': 0, "message": "用户添加失败,用户存在"}
self.client.publish(returnTopic, str(data).encode(), 1)

###################################################################
#taoyu
Expand Down

0 comments on commit 92f445d

Please sign in to comment.