forked from TooTallNate/Java-WebSocket
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChatClient.java
More file actions
129 lines (109 loc) · 3.79 KB
/
Copy pathChatClient.java
File metadata and controls
129 lines (109 loc) · 3.79 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
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
/**
* A barebones chat client that uses the WebSocket protocol.
*/
public class ChatClient extends WebSocketClient {
private final JTextArea ta;
public ChatClient(URI uri, JTextArea ta) {
super(uri);
this.ta = ta;
}
public void onMessage(String message) {
ta.append(message + "\n");
}
public void onOpen() {
ta.append("You are connected to ChatServer: " + getURI() + "\n");
}
public void onClose() {
ta.append("You have been disconnected from: " + getURI() + "\n");
}
/**
* The JFrame for our Chat client.
*/
private static class Frame extends JFrame implements ActionListener {
private final JTextField uriField;
private final JButton connect;
private final JTextArea area;
private final JTextField chatField;
private ChatClient cc;
public Frame() {
super("WebSocket Chat Client");
Container c = getContentPane();
GridLayout layout = new GridLayout();
layout.setColumns(1);
layout.setRows(4);
c.setLayout(layout);
uriField = new JTextField();
uriField.setText("ws://localhost");
c.add(uriField);
connect = new JButton("Connect");
connect.addActionListener(this);
c.add(connect);
JScrollPane scroll = new JScrollPane();
area = new JTextArea();
scroll.setViewportView(area);
c.add(scroll);
chatField = new JTextField();
chatField.setText("");
chatField.addActionListener(this);
c.add(chatField);
java.awt.Dimension d = new java.awt.Dimension(300, 400);
setPreferredSize(d);
setSize(d);
addWindowListener(new java.awt.event.WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
if (cc != null) {
try {
cc.close();
} catch (IOException ex) { ex.printStackTrace(); }
}
Frame.this.dispose();
}
});
setLocationRelativeTo(null);
setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == chatField) {
if (cc != null) {
try {
cc.send(chatField.getText());
chatField.setText("");
chatField.requestFocus();
} catch (IOException ex) { ex.printStackTrace(); }
}
} else if (e.getSource() == connect) {
connect.setEnabled(false);
uriField.setEditable(false);
try {
cc = new ChatClient(new URI(uriField.getText()), area);
cc.connect();
} catch (URISyntaxException ex) {
area.append(uriField.getText() + " is not a valid WebSocket URI\n");
connect.setEnabled(true);
uriField.setEditable(true);
}
}
}
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame();
}
});
}
}