first commit
This commit is contained in:
59
client1.py
Normal file
59
client1.py
Normal file
@@ -0,0 +1,59 @@
|
||||
import json
|
||||
import socket
|
||||
|
||||
|
||||
def chat(username, chat_obj, content):
|
||||
'''
|
||||
发送聊天信息
|
||||
'''
|
||||
pass
|
||||
|
||||
|
||||
# 创建socket
|
||||
ip_port = ("127.0.0.1", 9000)
|
||||
sk = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
|
||||
# 选择菜单
|
||||
while 1:
|
||||
print("menu".center(30, "="))
|
||||
print("1.login".center(30, " "))
|
||||
print("2.register".center(30, " "))
|
||||
print("=".center(30, "="))
|
||||
choice = input(">>>")
|
||||
|
||||
if choice == "1":
|
||||
username = input("用户名:")
|
||||
password = input("密码:")
|
||||
data = {
|
||||
"option": "login",
|
||||
"username": username,
|
||||
"password": password
|
||||
}
|
||||
sk.sendto(json.dumps(data).encode("utf-8"), ip_port)
|
||||
msg, addr = sk.recvfrom(1024)
|
||||
# {"status": 1}
|
||||
if json.loads(msg.decode("utf-8"))["status"]:
|
||||
chat_obj = input("输入收件人:")
|
||||
content = input("输入内容:")
|
||||
chat(username, chat_obj, content)
|
||||
continue
|
||||
else:
|
||||
print("登录失败")
|
||||
continue
|
||||
elif choice == "2":
|
||||
username = input("用户名:")
|
||||
password = input("密码:")
|
||||
data = {
|
||||
"option": "register",
|
||||
"username": username,
|
||||
"password": password
|
||||
}
|
||||
sk.sendto(json.dumps(data).encode("utf-8"), ip_port)
|
||||
msg, addr = sk.recvfrom(1024)
|
||||
# {"status": 1}
|
||||
if json.loads(msg.decode("utf-8"))["status"]:
|
||||
print("注册成功")
|
||||
continue
|
||||
else:
|
||||
print(json.loads(msg.decode("utf-8"))["msg"])
|
||||
continue
|
1
client2.py
Normal file
1
client2.py
Normal file
@@ -0,0 +1 @@
|
||||
import socket
|
28
readme.md
Normal file
28
readme.md
Normal file
@@ -0,0 +1,28 @@
|
||||
# 目前开发进度
|
||||
|
||||
## 服务端
|
||||
|
||||
- 可以实现接受用户的登录注册请求
|
||||
- 并且可以显示注册用户和当前在线用户
|
||||
|
||||
## 客户端
|
||||
|
||||
- 实现功能菜单选择
|
||||
- 实现登录注册的逻辑,可以与服务端通信
|
||||
|
||||
# 待实现的功能
|
||||
|
||||
## 服务端
|
||||
|
||||
- 将注册用户的信息持久化存储
|
||||
- 实现在线用户存活检测
|
||||
- 客户端3秒发送hello消息
|
||||
- 9秒未搜到客户端的hello消息,判断该用户已经下线
|
||||
- 实现聊天功能
|
||||
- 将需要发送到对应客户端的消息存在客户端对象中
|
||||
- 当收到客户端hello消息的时候,将未读消息发送过去
|
||||
|
||||
## 客户端
|
||||
|
||||
- 发送聊天信息
|
||||
- 接受未读消息
|
41
server.py
Normal file
41
server.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import json
|
||||
import socket
|
||||
|
||||
# 创建socket
|
||||
sk = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
|
||||
sk.bind(("0.0.0.0", 9000))
|
||||
active = []
|
||||
db = {}
|
||||
chat_msg = {}
|
||||
|
||||
while 1:
|
||||
print(f"当前活跃用户:{active} 当前注册用户:{db}")
|
||||
data, addr = sk.recvfrom(2048)
|
||||
data = json.loads(data)
|
||||
res = {"status": 0}
|
||||
if data["option"] == "login":
|
||||
username = data["username"]
|
||||
password = data["password"]
|
||||
if username in db:
|
||||
if db[username] == password:
|
||||
res = {"status": 1}
|
||||
active.append(data["username"])
|
||||
else:
|
||||
res = {"status": 0}
|
||||
else:
|
||||
res = {"status": 0}
|
||||
elif data["option"] == "register":
|
||||
username = data["username"]
|
||||
password = data["password"]
|
||||
if username in db:
|
||||
res = {"status": 0, "msg": "用户名已存在"}
|
||||
else:
|
||||
db[username] = password
|
||||
res = {"status": 1, "msg": "注册成功"}
|
||||
elif data["option"] == "chat":
|
||||
# 接受聊天信息,并且存入聊天临时字典
|
||||
pass
|
||||
elif data["option"] == "hello":
|
||||
# 接受客户端的存活状态,如果发现临时聊天字典有该用户的未收消息,发送过去
|
||||
pass
|
||||
sk.sendto(json.dumps(res).encode("utf-8"), addr)
|
Reference in New Issue
Block a user