我不到啊
This commit is contained in:
parent
cf611c11c3
commit
23dc75285f
55
CS3.1.py
55
CS3.1.py
@ -147,7 +147,7 @@ def chat():
|
|||||||
def broadcast_message(message, sender=None):
|
def broadcast_message(message, sender=None):
|
||||||
for conn in chat_connections:
|
for conn in chat_connections:
|
||||||
try:
|
try:
|
||||||
conn.sendall(json.dumps(message).encode())
|
conn.sendall(json.dumps(message).encode('utf-8'))
|
||||||
except:
|
except:
|
||||||
# 连接异常时移除
|
# 连接异常时移除
|
||||||
for uname, info in list(active_connections.items()):
|
for uname, info in list(active_connections.items()):
|
||||||
@ -163,7 +163,7 @@ def handle_socket_message(data, addr, conn):
|
|||||||
if action == 'register':
|
if action == 'register':
|
||||||
avatar = data.get('avatar', 'default_avatar.png')
|
avatar = data.get('avatar', 'default_avatar.png')
|
||||||
result = register_user(data.get('username'), data.get('password'), avatar)
|
result = register_user(data.get('username'), data.get('password'), avatar)
|
||||||
conn.sendall(json.dumps(result).encode())
|
conn.sendall(json.dumps(result).encode('utf-8'))
|
||||||
return result
|
return result
|
||||||
|
|
||||||
elif action == 'login':
|
elif action == 'login':
|
||||||
@ -177,7 +177,7 @@ def handle_socket_message(data, addr, conn):
|
|||||||
"status": "error",
|
"status": "error",
|
||||||
"message": "Account already logged in"
|
"message": "Account already logged in"
|
||||||
}
|
}
|
||||||
conn.sendall(json.dumps(response).encode())
|
conn.sendall(json.dumps(response).encode('utf-8'))
|
||||||
return response
|
return response
|
||||||
|
|
||||||
if isuserxist(username) and ispsswdright(username, password):
|
if isuserxist(username) and ispsswdright(username, password):
|
||||||
@ -203,7 +203,7 @@ def handle_socket_message(data, addr, conn):
|
|||||||
"username": username,
|
"username": username,
|
||||||
"avatar": avatar # 返回头像
|
"avatar": avatar # 返回头像
|
||||||
}
|
}
|
||||||
conn.sendall(json.dumps(response).encode())
|
conn.sendall(json.dumps(response).encode('utf-8'))
|
||||||
return response
|
return response
|
||||||
else:
|
else:
|
||||||
response = {
|
response = {
|
||||||
@ -211,18 +211,28 @@ def handle_socket_message(data, addr, conn):
|
|||||||
"status": "error",
|
"status": "error",
|
||||||
"message": "Invalid credentials"
|
"message": "Invalid credentials"
|
||||||
}
|
}
|
||||||
conn.sendall(json.dumps(response).encode())
|
conn.sendall(json.dumps(response).encode('utf-8'))
|
||||||
return response
|
return response
|
||||||
|
|
||||||
elif action == 'chat':
|
elif action == 'chat':
|
||||||
username = validate_token(data.get('token', ''))
|
token = data.get('token')
|
||||||
|
if not token:
|
||||||
|
response = {
|
||||||
|
"type": "chat",
|
||||||
|
"status": "error",
|
||||||
|
"message": "Missing token"
|
||||||
|
}
|
||||||
|
conn.sendall(json.dumps(response).encode('utf-8'))
|
||||||
|
return response
|
||||||
|
|
||||||
|
username = validate_token(token)
|
||||||
if not username:
|
if not username:
|
||||||
response = {
|
response = {
|
||||||
"type": "chat",
|
"type": "chat",
|
||||||
"status": "error",
|
"status": "error",
|
||||||
"message": "Not authenticated"
|
"message": "Invalid token"
|
||||||
}
|
}
|
||||||
conn.sendall(json.dumps(response).encode())
|
conn.sendall(json.dumps(response).encode('utf-8'))
|
||||||
return response
|
return response
|
||||||
|
|
||||||
if username not in active_connections:
|
if username not in active_connections:
|
||||||
@ -231,7 +241,7 @@ def handle_socket_message(data, addr, conn):
|
|||||||
"status": "error",
|
"status": "error",
|
||||||
"message": "Not logged in"
|
"message": "Not logged in"
|
||||||
}
|
}
|
||||||
conn.sendall(json.dumps(response).encode())
|
conn.sendall(json.dumps(response).encode('utf-8'))
|
||||||
return response
|
return response
|
||||||
|
|
||||||
message = {
|
message = {
|
||||||
@ -241,10 +251,18 @@ def handle_socket_message(data, addr, conn):
|
|||||||
"avatar": get_avatar(username) # 添加头像
|
"avatar": get_avatar(username) # 添加头像
|
||||||
}
|
}
|
||||||
broadcast_message(message)
|
broadcast_message(message)
|
||||||
return {"type": "chat", "status": "success"}
|
# 返回成功响应
|
||||||
|
response = {"type": "chat", "status": "success"}
|
||||||
|
conn.sendall(json.dumps(response).encode('utf-8'))
|
||||||
|
return response
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
return {"status": "error", "message": str(e)}
|
response = {
|
||||||
|
"status": "error",
|
||||||
|
"message": str(e)
|
||||||
|
}
|
||||||
|
conn.sendall(json.dumps(response).encode('utf-8'))
|
||||||
|
return response
|
||||||
|
|
||||||
def run_socket_server():
|
def run_socket_server():
|
||||||
socket_server.bind(("localhost", 8889))
|
socket_server.bind(("localhost", 8889))
|
||||||
@ -260,7 +278,9 @@ def run_socket_server():
|
|||||||
break
|
break
|
||||||
|
|
||||||
try:
|
try:
|
||||||
json_data = json.loads(data.decode())
|
# 尝试解码为UTF-8,忽略错误字符
|
||||||
|
decoded_data = data.decode('utf-8', errors='ignore')
|
||||||
|
json_data = json.loads(decoded_data)
|
||||||
response = handle_socket_message(json_data, addr, conn)
|
response = handle_socket_message(json_data, addr, conn)
|
||||||
except json.JSONDecodeError:
|
except json.JSONDecodeError:
|
||||||
response = {
|
response = {
|
||||||
@ -268,7 +288,14 @@ def run_socket_server():
|
|||||||
"status": "error",
|
"status": "error",
|
||||||
"message": "Invalid JSON"
|
"message": "Invalid JSON"
|
||||||
}
|
}
|
||||||
conn.sendall(json.dumps(response).encode())
|
conn.sendall(json.dumps(response).encode('utf-8'))
|
||||||
|
except Exception as e:
|
||||||
|
response = {
|
||||||
|
"type": "error",
|
||||||
|
"status": "error",
|
||||||
|
"message": str(e)
|
||||||
|
}
|
||||||
|
conn.sendall(json.dumps(response).encode('utf-8'))
|
||||||
except (ConnectionResetError, BrokenPipeError):
|
except (ConnectionResetError, BrokenPipeError):
|
||||||
print(f"Client {addr} disconnected abruptly")
|
print(f"Client {addr} disconnected abruptly")
|
||||||
finally:
|
finally:
|
||||||
@ -294,6 +321,6 @@ if __name__ == '__main__':
|
|||||||
conn.execute('''CREATE TABLE IF NOT EXISTS users
|
conn.execute('''CREATE TABLE IF NOT EXISTS users
|
||||||
(name TEXT PRIMARY KEY,
|
(name TEXT PRIMARY KEY,
|
||||||
passwd TEXT,
|
passwd TEXT,
|
||||||
avatar TEXT)''')
|
avatar TEXT DEFAULT 'default_avatar.png')''')
|
||||||
threading.Thread(target=run_socket_server, daemon=True).start()
|
threading.Thread(target=run_socket_server, daemon=True).start()
|
||||||
app.run(port=5001)
|
app.run(port=5001)
|
Loading…
x
Reference in New Issue
Block a user