准备获取联系人列表(数据库),客户端重复发送

This commit is contained in:
lsy2246 2024-04-18 01:14:04 +08:00
parent 8c212e3803
commit c93c289ef4
5 changed files with 104 additions and 34 deletions

View File

View File

@ -3,12 +3,17 @@ import wx.aui
import wx.lib.scrolledpanel as scrolled
import time
import multiprocessing
import os
import csv
from Client.Transmission.Process_Client import ProcessClient
class ChatFrame(wx.Frame):
def __init__(self):
super().__init__(None, size=(800, 600), title="客户端")
class ChatFrame(wx.Frame, ProcessClient):
def __init__(self, Id):
wx.Frame().__init__(None, size=(800, 600), title="账号: " + str(Id))
ProcessClient.__init__(self)
self.detection_data(Id)
ChatMain_Panel = wx.Panel(self, style=wx.BORDER_SUNKEN)
ChatMain_box = wx.BoxSizer(wx.HORIZONTAL)
@ -113,6 +118,30 @@ class ChatFrame(wx.Frame):
self.find_page.Show()
self.Layout()
def detection_data(self, Id):
root_path = rf'.\{Id}'
info_path = root_path + r'\info.csv'
file_root_path = root_path + r'\file'
other_file_path = file_root_path + r'\other'
image_file_path = file_root_path + r'\image'
if not os.path.isdir(root_path):
os.mkdir(root_path)
if not os.path.isdir(file_root_path):
os.mkdir(file_root_path)
os.mkdir(other_file_path)
os.mkdir(image_file_path)
if not os.path.exists(info_path):
with open(info_path, 'w', encoding='utf-8') as f:
pass
with open(info_path, 'r+', encoding='utf-8') as info:
if info.tell() == 0:
date = None
target = "服务器"
genre = "数据更新"
content = date
data = {"genre": genre, "target": target, "content": content}
self.Process_client_send("Session_server", "send_server", data)
class ChatPage(wx.Panel):
def __init__(self, parent):
wx.Panel.__init__(self, parent, style=wx.BORDER_SUNKEN)
@ -196,6 +225,7 @@ class ChatFrame(wx.Frame):
chat_panel.SetSizer(chat_box)
self.chat_tab.AddPage(chat_panel, Remark, select=True)
self.GetParent().contact_windows[Id] = chat_panel
class ConnectPage(wx.Panel):
def __init__(self, parent):

View File

@ -9,9 +9,9 @@ from Client.Transmission.Process_Client import ProcessClient
from .Chat_main import ChatFrame
def open_chat_window():
def open_chat_window(Id):
app = wx.App()
ChatFrame().Show()
ChatFrame(Id).Show()
app.MainLoop()
@ -20,6 +20,7 @@ class LoginFrame(wx.Frame, ProcessClient):
wx.Frame.__init__(self, None, id=-1, title='登录', pos=wx.DefaultPosition, size=(380, 300))
ProcessClient.__init__(self)
current_file_path = __file__
current_file_name = os.path.basename(current_file_path).split('.')[0]
self.Process_client_send("Server", "Name", current_file_name)
@ -107,10 +108,10 @@ class LoginFrame(wx.Frame, ProcessClient):
def login_page_receive(self, receive_content):
if receive_content["genre"] == '登录':
match receive_content["data"]:
match receive_content["data"]['status']:
case 0:
multiprocessing.Process(target=open_chat_window).start()
self.Close()
self.Destroy()
multiprocessing.Process(target=open_chat_window, args=(receive_content["data"]['account'],)).start()
case -1:
wx.MessageBox('重复登录', '登录', wx.OK | wx.ICON_INFORMATION)
case 1:
@ -130,7 +131,9 @@ class LoginFrame(wx.Frame, ProcessClient):
target = "服务器"
genre = "登录"
data = {"genre": genre, "target": target, "content": content}
self.Process_client_send("Session_server", "send_server", data)
if account and password:
self.Process_client_send("Session_server", "send_server", data)
time.sleep(3)
def send_register_button(self, event):
if self.server_status:
@ -140,7 +143,9 @@ class LoginFrame(wx.Frame, ProcessClient):
target = "服务器"
genre = "注册"
data = {"genre": genre, "target": target, "content": content}
self.Process_client_send("Session_server", "send_server", data)
if account and password:
self.Process_client_send("Session_server", "send_server", data)
time.sleep(3)
def Process_client_pick(self, data):
if data['target'] in ['ALL', 'Login']:
@ -151,6 +156,7 @@ class LoginFrame(wx.Frame, ProcessClient):
self.login_page_receive(data['content'])
class LoginPanel(wx.Panel):
def __init__(self, parent):
super().__init__(parent)

View File

@ -89,21 +89,29 @@ class database(ProcessClient):
if data['target'] in ['ALL', 'Database_formula']:
match data['function']:
case 'check_account_state':
client_socket = data['content']['client_socket']
client_id = data['content']['client_id']
account = data['content']['account']
password = data['content']['password']
status = self.check_account_state(account, password)
content = {"account": account, "status": status}
content = {"client_socket": client_socket, "genre": "客户端", "target": "登录", "content": content}
content = {"client_id": client_id, "target": "客户端", "genre": "登录", "data": content}
self.Process_client_send('Session_client', 'send_client', content)
case 'sign_account':
client_socket = data['content']['client_socket']
client_id = data['content']['client_id']
account = data['content']['account']
password = data['content']['password']
info = self.sign_account(account, password)
content = {"client_socket": client_socket, "genre": "客户端", "target": "注册", "content": info}
content = {"client_id": client_id, "target": "客户端", "genre": "注册", "data": info}
self.Process_client_send('Session_client', 'send_client', content)
case 'alter_state_database':
self.alter_state_database(data['content']['Id'], data['content']['sate'])
def detection_data(self,date):
if date is None:
self.database_cursor.execute(f"update Account set State = N'{sate}' where Id = {Id}")

View File

@ -1,4 +1,6 @@
import os
import random
import string
import time
import socket
import json
@ -15,6 +17,7 @@ class link_client(ProcessClient):
self.server_socket.bind((self.server_host, self.server_port))
self.server_socket.listen()
self.client_socket_dict = {}
self.client_max_id = 0
current_file_path = __file__
current_file_name = os.path.basename(current_file_path).split('.')[0]
@ -27,12 +30,16 @@ class link_client(ProcessClient):
while True:
try:
client_socket, client_address = self.server_socket.accept()
self.client_max_id += 1
client_id = ''.join(random.choice(string.ascii_letters) for _ in range(10)) + str(self.client_max_id)
self.client_socket_dict[client_id] = client_socket
recv_client_Thread = threading.Thread(target=self.recv_client(client_socket), daemon=True)
recv_client_Thread.start()
except:
pass
def send_client(self, client_socket, genre, target, content):
def send_client(self, client_id, genre, target, content):
client_socket = self.client_socket_dict[client_id]
try:
data = {"genre": genre, "target": target, "data": content,
"datetime": time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())}
@ -41,16 +48,18 @@ class link_client(ProcessClient):
except:
client_socket.close()
def pick_data(self, client_socket, data):
def pick_data(self, client_id, data):
match data['genre']:
case '登录':
content = {'client_socket': client_socket, 'account': data['data']['account'],
content = {'client_id': client_id, 'account': data['data']['account'],
'password': data['data']['password']}
self.Process_client_send("Database_formula", "check_account_state", content)
case '注册':
content = {'client_socket': client_socket, 'account': data['data']['account'],
content = {'client_id': client_id, 'account': data['data']['account'],
'password': data['data']['password']}
self.Process_client_send("Database_formula", "sign_account", content)
case '数据更新':
self.Process_client_send("Database_formula", "sign_account", data['data'])
def recv_client(self, client_socket):
state = True
@ -58,31 +67,48 @@ class link_client(ProcessClient):
try:
data_json = client_socket.recv(1024).decode('utf-8')
data = json.loads(data_json)
self.pick_data(client_socket, data)
client_Id = self.find_client(client_socket, None)
self.pick_data(client_Id, data)
except:
try:
client_socket.close()
finally:
state = False
for key, value in self.client_socket_dict.items():
if value == client_socket:
del self.client_socket_dict[key]
self.Process_client_send("Database_formula", "alter_state_database",
{"Id": key, "sate": "在线"})
break
self.find_client(client_socket, None)
client_id = self.find_client(client_socket, None)
del self.client_socket_dict[client_id]
if client_id.isdigit():
self.Process_client_send("Database_formula", "alter_state_database",
{"Id": client_id, "sate": 0})
def Process_client_pick(self, data):
if data['target'] in ['ALL', 'Session_client']:
match data['function']:
case 'send_client':
client_socket = data['content']['client_socket']
client_id = data['content']['client_id']
genre = data['content']['genre']
target = data['content']['target']
content = data['content']['content']
self.send_client(client_socket, genre, target, content)
if target == '登录':
if data['content']['status'] == 0:
self.client_socket_dict[data['content']['account']] = data['data']['client_socket']
self.Process_client_send("Database_formula", "alter_state_database",
{"Id": data['content']['account'],
"sate": "在线"})
content = data['content']['data']
if data['content']['genre'] == '登录':
if content['account'] not in self.client_socket_dict:
if content['status'] == 0:
client_socket = self.find_client(None, client_id)
del self.client_socket_dict[client_id]
client_id = content['account']
self.client_socket_dict[client_id] = client_socket
if client_id.isdigit():
self.Process_client_send("Database_formula", "alter_state_database",
{"Id": client_id, "sate": 1})
else:
return
self.send_client(client_id, genre, target, content)
def find_client(self, client_socket, client_id):
if client_id is None:
for key, value in self.client_socket_dict.items():
if value == client_socket:
return key
elif client_socket is None:
for key, value in self.client_socket_dict.items():
if key == client_id:
return value