-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.py
145 lines (108 loc) · 4.53 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
from PyQt5 import QtCore, QtWidgets
import register2
import chat
#from server import *
import sys
import socket
import random
class ReceiveThread(QtCore.QThread):
signal = QtCore.pyqtSignal(str)
def __init__(self, client_socket):
super(ReceiveThread, self).__init__()
self.client_socket = client_socket
def run(self):
while True:
self.receive_message()
def receive_message(self):
message = self.client_socket.recv(1024)
message = message.decode()
print(message)
self.signal.emit(message)
class Client(object):
def __init__(self):
self.messages = []
self.mainWindow = QtWidgets.QMainWindow()
self.connectWidget = QtWidgets.QWidget(self.mainWindow)
self.chatWidget = QtWidgets.QWidget(self.mainWindow)
self.chatWidget.setHidden(True)
self.chat_ui = chat.Ui_MainWindow()
self.chat_ui.setupUi(self.chatWidget)
self.chat_ui.pushButton.clicked.connect(self.send_message)
self.connect_ui = register2.Ui_MainWindow()
self.connect_ui.setupUi(self.connectWidget)
self.connect_ui.pushButton.clicked.connect(self.btn_connect_clicked)
# MainWindow.setCentralWidget(self.centralwidget)
# self.menubar = QtWidgets.QMenuBar(MainWindow)
# self.menubar.setGeometry(QtCore.QRect(0, 0, 904, 9))
# self.menubar.setObjectName("menubar")
# MainWindow.setMenuBar(self.menubar)
# self.statusbar = QtWidgets.QStatusBar(MainWindow)
# self.statusbar.setObjectName("statusbar")
# MainWindow.setStatusBar(self.statusbar)
self.mainWindow.setGeometry(QtCore.QRect(200, 200, 930, 780)) #200 200 930 780
self.mainWindow.show()
self.tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
def btn_connect_clicked(self):
host = self.connect_ui.lineEdit.text()
port = self.connect_ui.lineEdit_2.text()
name = self.connect_ui.lineEdit_3.text()
#id = self.connect_ui.lineEdit_4.text()
if len(host) == 0:
host = "localhost"
if len(port) == 0:
port = 5555
else:
try:
port = int(port)
except Exception as e:
error = "Invalid port number \n'{}'".format(str(e))
print("[INFO]", error)
self.show_error("Port Number Error", error)
if len(name) < 1:
name = socket.gethostname()
name = name + "_" + str(random.randint(1, port))
if self.connect(host, port, name):
self.connectWidget.setHidden(True)
self.chatWidget.setVisible(True)
self.recv_thread = ReceiveThread(self.tcp_client)
self.recv_thread.signal.connect(self.show_message)
self.recv_thread.start()
print("[INFO] recv thread started")
#print(len(clients))
#self.chat_ui.textBrowser_2.append(server.totalClients())
def show_message(self, message):
self.chat_ui.textBrowser.append(message)
def connect(self, host, port, name):
try:
self.tcp_client.connect((host, port))
self.tcp_client.send(name.encode())
print("[INFO] Connected to server")
return True
except Exception as e:
error = "Unable to connect to server \n'{}'".format(str(e))
print("[INFO]", error)
self.show_error("Connection Error", error)
self.connect_ui.lineEdit.clear()
self.connect_ui.lineEdit_2.clear()
return False
def send_message(self):
message = self.chat_ui.lineEdit.text()
self.chat_ui.textBrowser.append("Me: " + message)
print("sent: " + message)
try:
self.tcp_client.send(message.encode())
except Exception as e:
error = "Unable to send message '{}'".format(str(e))
print("[INFO]", error)
self.show_error("Server Error", error)
self.chat_ui.lineEdit.clear()
def show_error(self, error_type, message):
errorDialog = QtWidgets.QMessageBox()
errorDialog.setText(message)
errorDialog.setWindowTitle(error_type)
errorDialog.setStandardButtons(QtWidgets.QMessageBox.Ok)
errorDialog.exec_()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
c = Client()
sys.exit(app.exec())