-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclient_frame.java
More file actions
314 lines (271 loc) · 11.2 KB
/
client_frame.java
File metadata and controls
314 lines (271 loc) · 11.2 KB
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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
package chat_client;
import java.net.*;
import java.io.*;
import java.util.*;
public class client_frame extends javax.swing.JFrame
{
//Configuração
String username, address = "localhost";
ArrayList<String> users = new ArrayList();
int port = 3000;
Boolean isConnected = false;
byte[] server = {/*add ip*/};
InetAddress serverAddr;
Socket websocket;
BufferedReader reader;
PrintWriter writer;
//--------------------------//
public void ListenThread()
{
Thread IncomingReader = new Thread(new IncomingReader());
IncomingReader.start();
}
//--------------------------//
public void userAdd(String data)
{
users.add(data);
}
//--------------------------//
public void userRemove(String data)
{
ta_chat.append(data + " está online.\n");
}
//--------------------------//
public void writeUsers()
{
String[] tempList = new String[(users.size())];
users.toArray(tempList);
for (String token:tempList)
{
//users.append(token + "\n");
}
}
//--------------------------//
public void sendDisconnect()
{
String bye = (username + ": :Disconectado");
try
{
writer.println(bye);
writer.flush();
} catch (Exception e)
{
ta_chat.append("Could not send Disconnect message.\n");
}
}
//--------------------------//
public void Disconnect()
{
try
{
ta_chat.append("Disconectado.\n");
websocket.close();
} catch(Exception ex) {
ta_chat.append("Falha em disconectar. \n");
}
isConnected = false;
tf_username.setEditable(true);
}
public client_frame()
{
initComponents();
}
//--------------------------//
public class IncomingReader implements Runnable
{
@Override
public void run()
{
String[] data;
String stream, done = "Done", connect = "Connect", disconnect = "Disconectado", chat = "Chat";
try
{
while ((stream = reader.readLine()) != null)
{
data = stream.split(":");
if (data[2].equals(chat))
{
ta_chat.append("[" + data[0] + "] " + data[1] + "\n");
ta_chat.setCaretPosition(ta_chat.getDocument().getLength());
System.out.println(data[0] + ": " + data[1] + "\n");
}
else if (data[2].equals(connect))
{
ta_chat.removeAll();
userAdd(data[0]);
}
else if (data[2].equals(disconnect))
{
userRemove(data[0]);
}
else if (data[2].equals(done))
{
//users.setText("");
writeUsers();
users.clear();
}
}
}catch(Exception ex) { }
}
}
//--------------------------//
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
private void initComponents() {
lb_username = new javax.swing.JLabel();
tf_username = new javax.swing.JTextField();
b_connect = new javax.swing.JButton();
b_disconnect = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
ta_chat = new javax.swing.JTextArea();
tf_chat = new javax.swing.JTextField();
b_send = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("Chat - Client's frame");
setName("client"); // NOI18N
setResizable(false);
lb_username.setText("Username :");
tf_username.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
tf_usernameActionPerformed(evt);
}
});
b_connect.setText("Connect");
b_connect.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b_connectActionPerformed(evt);
}
});
b_disconnect.setText("Disconnect");
b_disconnect.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b_disconnectActionPerformed(evt);
}
});
ta_chat.setColumns(20);
ta_chat.setRows(5);
jScrollPane1.setViewportView(ta_chat);
b_send.setText("SEND");
b_send.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
b_sendActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addComponent(tf_chat, javax.swing.GroupLayout.PREFERRED_SIZE, 352, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(b_send, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addComponent(jScrollPane1)
.addGroup(layout.createSequentialGroup()
.addComponent(lb_username, javax.swing.GroupLayout.PREFERRED_SIZE, 62, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(tf_username, javax.swing.GroupLayout.PREFERRED_SIZE, 165, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(18, 18, 18)
.addComponent(b_connect, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(b_disconnect, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(0, 0, Short.MAX_VALUE)))
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addContainerGap()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(tf_username)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(lb_username)
.addComponent(b_connect)
.addComponent(b_disconnect)))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 335, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(tf_chat)
.addComponent(b_send, javax.swing.GroupLayout.DEFAULT_SIZE, 59, Short.MAX_VALUE))
.addGap(10, 10, 10))
);
pack();
}// </editor-fold>//GEN-END:initComponents
private void b_connectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_connectActionPerformed
if (isConnected == false)
{
username = tf_username.getText();
tf_username.setEditable(false);
try
{
serverAddr = InetAddress.getByAddress(server);
websocket = new Socket(serverAddr, port);
InputStreamReader streamreader = new InputStreamReader(websocket.getInputStream());
reader = new BufferedReader(streamreader);
writer = new PrintWriter(websocket.getOutputStream());
writer.println(username + ":Conectou.:Connect");
writer.flush();
isConnected = true;
}
catch (Exception ex)
{
ta_chat.append("Falha em conectar. \n");
tf_username.setEditable(true);
}
ListenThread();
} else if (isConnected == true)
{
ta_chat.append("Você já está conectado. \n");
}
}//GEN-LAST:event_b_connectActionPerformed
private void b_disconnectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_disconnectActionPerformed
sendDisconnect();
Disconnect();
}//GEN-LAST:event_b_disconnectActionPerformed
/**/
private void b_sendActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_b_sendActionPerformed
String nothing = "";
if ((tf_chat.getText()).equals(nothing)) {
tf_chat.setText("");
tf_chat.requestFocus();
} else {
try {
writer.println(username + ":" + tf_chat.getText() + ":" + "Chat");
writer.flush(); // flushes the buffer
} catch (Exception ex) {
ta_chat.append("A mensagem não foi enviada. \n");
}
tf_chat.setText("");
tf_chat.requestFocus();
}
tf_chat.setText("");
tf_chat.requestFocus();
}//GEN-LAST:event_b_sendActionPerformed
private void tf_usernameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tf_usernameActionPerformed
}//GEN-LAST:event_tf_usernameActionPerformed
/**/
public static void main(String args[])
{
java.awt.EventQueue.invokeLater(new Runnable()
{
@Override
public void run()
{
new client_frame().setVisible(true);
}
});
}
// Variables declaration - do not modify//GEN-BEGIN:variables
private javax.swing.JButton b_connect;
private javax.swing.JButton b_disconnect;
private javax.swing.JButton b_send;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JLabel lb_username;
private javax.swing.JTextArea ta_chat;
private javax.swing.JTextField tf_chat;
private javax.swing.JTextField tf_username;
// End of variables declaration//GEN-END:variables
}