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
148 lines (125 loc) · 4.47 KB
/
ChatClient.java
File metadata and controls
148 lines (125 loc) · 4.47 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
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;
import net.tootallnate.websocket.WebSocketClient;
import net.tootallnate.websocket.WebSocketDraft;
/**
* A barebones chat client that uses the WebSocket protocol.
*/
public class ChatClient extends WebSocketClient {
private final JTextArea ta;
public ChatClient(URI uri, JTextArea ta, WebSocketDraft draft) {
super(uri, draft);
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");
}
public void onIOError(IOException ex) {
ta.append("Network problem ...\n");
}
/**
* The JFrame for our Chat client.
*/
private static class Frame extends JFrame implements ActionListener {
private static final long serialVersionUID = -6056260699202978657L;
private final JTextField uriField;
private final JButton connect;
private final JButton close;
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(5);
c.setLayout(layout);
uriField = new JTextField();
uriField.setText("ws://localhost:8887");
c.add(uriField);
connect = new JButton("Connect");
connect.addActionListener(this);
c.add(connect);
close = new JButton("Close");
close.addActionListener(this);
c.add(close);
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, WebSocketDraft.DRAFT76);
cc.connect();
} catch (URISyntaxException ex) {
area.append(uriField.getText() + " is not a valid WebSocket URI\n");
connect.setEnabled(true);
uriField.setEditable(true);
}
} else if (e.getSource() == close) {
try {
cc.close();
} catch(Exception ex) {
ex.printStackTrace();
}
}
}
}
public static void main(String[] args) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new Frame();
}
});
}
}