-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainUser.java
267 lines (228 loc) · 10.4 KB
/
MainUser.java
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Scanner;
public class MainUser {
private final JTextArea chatArea;
private final JTextField inputField;
private OutputStream outputStream;
private boolean typedSomething = false;
public MainUser(String username, String ngrokUrl) {
// Create the main application window
JFrame frame = new JFrame("Chat App - " + username);
// Define a modern font to be used in the UI
Font modernFont = new Font("Segoe UI", Font.PLAIN, 16);
// Initialize the chat area where messages will be displayed
chatArea = new JTextArea();
chatArea.setFont(modernFont);
chatArea.setBackground(new Color(40, 44, 52));
chatArea.setForeground(Color.WHITE);
chatArea.setEditable(false);
chatArea.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// Initialize the input field where users will type their messages
inputField = new JTextField(30);
inputField.setFont(modernFont);
inputField.setBackground(new Color(50, 54, 62));
inputField.setForeground(Color.WHITE);
inputField.setCaretColor(Color.WHITE);
inputField.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
// Create the send button
JButton sendButton = new JButton("Send");
sendButton.setFont(modernFont);
sendButton.setBackground(new Color(0, 123, 255));
sendButton.setForeground(Color.WHITE);
sendButton.setFocusPainted(false);
sendButton.setBorder(BorderFactory.createEmptyBorder(10, 20, 10, 20));
sendButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
// Add mouse listener to change button color on hover
sendButton.addMouseListener(new MouseAdapter() {
@Override
public void mouseEntered(MouseEvent e) {
sendButton.setBackground(new Color(0, 150, 255));
}
@Override
public void mouseExited(MouseEvent e) {
sendButton.setBackground(new Color(0, 123, 255));
}
});
// Placeholder text handling for the input field
inputField.setText("Type your message");
inputField.setForeground(Color.GRAY);
inputField.addFocusListener(new FocusAdapter() {
@Override
public void focusGained(FocusEvent e) {
if (inputField.getText().equals("Type your message")) {
inputField.setText("");
inputField.setForeground(Color.WHITE);
}
}
@Override
public void focusLost(FocusEvent e) {
if (inputField.getText().isEmpty()) {
inputField.setText("Type your message");
inputField.setForeground(Color.GRAY);
typedSomething = false;
}
}
});
// Action listener for the send button
ActionListener sendAction = new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String message = inputField.getText();
if (typedSomething && !message.isEmpty()) {
sendMessage(message, username);
inputField.setText("");
typedSomething = false;
}
}
};
sendButton.addActionListener(sendAction);
// Key listener for the input field to handle Enter key press
inputField.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent e) {
if (!inputField.getText().equals("Type your message")) {
typedSomething = true;
}
if (e.getKeyCode() == KeyEvent.VK_ENTER) {
sendAction.actionPerformed(null);
}
}
});
// Layout for the input panel
JPanel inputPanel = new JPanel(new BorderLayout());
inputPanel.setBackground(new Color(30, 34, 42));
inputPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
inputPanel.add(inputField, BorderLayout.CENTER);
inputPanel.add(sendButton, BorderLayout.EAST);
// Layout for the chat panel
JPanel chatPanel = new JPanel(new BorderLayout());
chatPanel.setBackground(new Color(30, 34, 42));
chatPanel.add(new JScrollPane(chatArea), BorderLayout.CENTER);
chatPanel.add(inputPanel, BorderLayout.SOUTH);
// Set up the main frame with the chat panel
frame.setLayout(new BorderLayout());
frame.add(chatPanel, BorderLayout.CENTER);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(1280, 720);
frame.setVisible(true);
// Connect to the server using the provided ngrok URL
connectToServer(ngrokUrl);
// Timer to refresh chat history every second
// Timer refreshTimer = new Timer(1000, (e) -> refreshChatHistory());
// refreshTimer.start();
}
// Method to connect to the server using the provided ngrok URL
private void connectToServer(String ngrokUrl) {
try {
URI uri = new URI(ngrokUrl);
String host = uri.getHost();
int port = uri.getPort() == -1 ? 80 : uri.getPort();
Socket socket = new Socket(host, port);
outputStream = socket.getOutputStream();
InputStream inputStream = socket.getInputStream();
// WebSocket handshake request
String handshake = "GET / HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Upgrade: websocket\r\n" +
"Connection: Upgrade\r\n" +
"Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==\r\n" +
"Sec-WebSocket-Version: 13\r\n\r\n";
outputStream.write(handshake.getBytes(StandardCharsets.UTF_8));
outputStream.flush();
byte[] buffer = new byte[4096];
int bytesRead = inputStream.read(buffer);
String response = new String(buffer, 0, bytesRead, StandardCharsets.UTF_8);
// Check if the server accepted the WebSocket handshake
if (response.contains("HTTP/1.1 101 Switching Protocols")) {
chatArea.append("Connected to server\n");
// Start a new thread to listen for incoming messages
new Thread(() -> {
try {
while (true) {
int firstByte = inputStream.read();
if (firstByte == -1) break;
int secondByte = inputStream.read();
int payloadLength = secondByte & 0x7F;
if (payloadLength == 126) {
byte[] extended = new byte[2];
inputStream.read(extended, 0, 2);
payloadLength = ((extended[0] & 0xFF) << 8) | (extended[1] & 0xFF);
} else if (payloadLength == 127) {
byte[] extended = new byte[8];
inputStream.read(extended, 0, 8);
payloadLength = 0;
for (int i = 0; i < 8; i++) {
payloadLength = (payloadLength << 8) | (extended[i] & 0xFF);
}
}
byte[] payload = new byte[payloadLength];
inputStream.read(payload, 0, payloadLength);
String receivedMessage = new String(payload, StandardCharsets.UTF_8);
SwingUtilities.invokeLater(() -> chatArea.append(receivedMessage + "\n"));
}
} catch (Exception e) {
e.printStackTrace();
}
}).start();
} else {
chatArea.append("Failed to connect to server\n");
}
} catch (Exception e) {
e.printStackTrace();
}
}
// Method to send a message to the server
private void sendMessage(String message, String username) {
try {
String formattedMessage = username + ": " + message;
byte[] messageBytes = formattedMessage.getBytes(StandardCharsets.UTF_8);
outputStream.write(new byte[]{(byte) 0x81, (byte) messageBytes.length});
outputStream.write(messageBytes);
outputStream.flush();
} catch (Exception e) {
e.printStackTrace();
}
}
// Method to refresh chat history from the local storage file
// private void refreshChatHistory() {
// try {
// if (Files.exists(Paths.get("history.json"))) {
// String content = new String(Files.readAllBytes(Paths.get("history.json")), StandardCharsets.UTF_8);
// String[] messagesArray = content.substring(1, content.length() - 1).split("\",\"");
// StringBuilder historyBuilder = new StringBuilder("Connected to server\n");
// for (String message : messagesArray) {
// message = message.replaceAll("^\"|\"$", "").replace("\\\"", "\"");
// historyBuilder.append(message).append("\n");
// }
// chatArea.setText(historyBuilder.toString());
// } else {
// chatArea.setText("Connected to server\n");
// }
// } catch (Exception e) {
// e.printStackTrace();
// }
// }
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter username: ");
String username = sc.nextLine();
// Start the WebSocket server in a new thread
new Thread(() -> {
WebsocketServer.main(new String[]{});
}).start();
System.out.print("Enter the ngrok URL: ");
String ngrokUrl = sc.nextLine();
// Start the main user interface
SwingUtilities.invokeLater(() -> new MainUser(username, ngrokUrl));
sc.close();
}
}