-
Notifications
You must be signed in to change notification settings - Fork 0
/
GWackClientListener.java
74 lines (67 loc) · 2.46 KB
/
GWackClientListener.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
import java.net.*;
import javax.swing.*;
import java.io.*;
public class GWackClientListener extends Thread {
private Socket socket;
private JTextArea textArea;
private JList<String> uList;
public GWackClientListener(Socket socket, JTextArea textArea, JList<String> uList) {
this.socket = socket;
this.textArea = textArea;
this.uList = uList;
}
public void run() {
try {
PrintWriter pw = new PrintWriter(socket.getOutputStream());
BufferedReader in =
new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String line;
boolean message = false;
boolean userList = false;
String users = "";
while ((line = in.readLine()) != null) {
// Check for message
if (line.equals("START MSG")) {
// Start message, continue to next line
message = true;
continue;
}
else if (line.equals("END MSG")) {
// End message, continue to next line
textArea.setCaretPosition(textArea.getDocument().getLength()); // Scroll to bottom
message = false;
continue;
}
else if (message) {
// Add message to text area
textArea.append(line + "\n");
continue;
}
// Check for update of user list
if (line.equals("START_CLIENT_LIST")) {
// Start user list, continue to next line
userList = true;
continue;
}
else if (line.equals("END_CLIENT_LIST")) {
// End update of users
userList = false;
users = users.replaceAll("^,|,$", ""); // Remove first and last comma
// Update user list
uList.setListData(users.split(","));
users = "";
continue;
}
else if (userList) {
// Add to user list
users += "," + line;
continue;
}
}
pw.close(); //close the stream
in.close();//close the input stream
}
catch(Exception e) {}
}
}