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
184 lines (159 loc) · 5.54 KB
/
ChatClient.java
File metadata and controls
184 lines (159 loc) · 5.54 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
/*
* Copyright (c) 2010-2019 Nathan Rajlich
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use,
* copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following
* conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
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.net.URI;
import java.net.URISyntaxException;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import org.java_websocket.WebSocketImpl;
import org.java_websocket.client.WebSocketClient;
import org.java_websocket.drafts.Draft;
import org.java_websocket.drafts.Draft_6455;
import org.java_websocket.handshake.ServerHandshake;
public class ChatClient 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 ta;
private final JTextField chatField;
private final JComboBox draft;
private WebSocketClient cc;
public ChatClient( String defaultlocation ) {
super( "WebSocket Chat Client" );
Container c = getContentPane();
GridLayout layout = new GridLayout();
layout.setColumns( 1 );
layout.setRows( 6 );
c.setLayout( layout );
Draft[] drafts = { new Draft_6455() };
draft = new JComboBox( drafts );
c.add( draft );
uriField = new JTextField();
uriField.setText( defaultlocation );
c.add( uriField );
connect = new JButton( "Connect" );
connect.addActionListener( this );
c.add( connect );
close = new JButton( "Close" );
close.addActionListener( this );
close.setEnabled( false );
c.add( close );
JScrollPane scroll = new JScrollPane();
ta = new JTextArea();
scroll.setViewportView( ta );
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 ) {
cc.close();
}
dispose();
}
} );
setLocationRelativeTo( null );
setVisible( true );
}
public void actionPerformed( ActionEvent e ) {
if( e.getSource() == chatField ) {
if( cc != null ) {
cc.send( chatField.getText() );
chatField.setText( "" );
chatField.requestFocus();
}
} else if( e.getSource() == connect ) {
try {
// cc = new ChatClient(new URI(uriField.getText()), area, ( Draft ) draft.getSelectedItem() );
cc = new WebSocketClient( new URI( uriField.getText() ), (Draft) draft.getSelectedItem() ) {
@Override
public void onMessage( String message ) {
ta.append( "got: " + message + "\n" );
ta.setCaretPosition( ta.getDocument().getLength() );
}
@Override
public void onOpen( ServerHandshake handshake ) {
ta.append( "You are connected to ChatServer: " + getURI() + "\n" );
ta.setCaretPosition( ta.getDocument().getLength() );
}
@Override
public void onClose( int code, String reason, boolean remote ) {
ta.append( "You have been disconnected from: " + getURI() + "; Code: " + code + " " + reason + "\n" );
ta.setCaretPosition( ta.getDocument().getLength() );
connect.setEnabled( true );
uriField.setEditable( true );
draft.setEditable( true );
close.setEnabled( false );
}
@Override
public void onError( Exception ex ) {
ta.append( "Exception occured ...\n" + ex + "\n" );
ta.setCaretPosition( ta.getDocument().getLength() );
ex.printStackTrace();
connect.setEnabled( true );
uriField.setEditable( true );
draft.setEditable( true );
close.setEnabled( false );
}
};
close.setEnabled( true );
connect.setEnabled( false );
uriField.setEditable( false );
draft.setEditable( false );
cc.connect();
} catch ( URISyntaxException ex ) {
ta.append( uriField.getText() + " is not a valid WebSocket URI\n" );
}
} else if( e.getSource() == close ) {
cc.close();
}
}
public static void main( String[] args ) {
String location;
if( args.length != 0 ) {
location = args[ 0 ];
System.out.println( "Default server url specified: \'" + location + "\'" );
} else {
location = "ws://localhost:8887";
System.out.println( "Default server url not specified: defaulting to \'" + location + "\'" );
}
new ChatClient( location );
}
}