88import java .net .URISyntaxException ;
99
1010import javax .swing .JButton ;
11+ import javax .swing .JComboBox ;
1112import javax .swing .JFrame ;
13+ import javax .swing .JMenu ;
1214import javax .swing .JScrollPane ;
1315import javax .swing .JTextArea ;
1416import javax .swing .JTextField ;
1517
1618import net .tootallnate .websocket .Draft ;
1719import net .tootallnate .websocket .WebSocketClient ;
1820import net .tootallnate .websocket .drafts .Draft_10 ;
19-
20- /**
21- * A barebones chat client that uses the WebSocket protocol.
22- */
23- public class ChatClient extends WebSocketClient {
24- private final JTextArea ta ;
25-
26- public ChatClient (URI uri , JTextArea ta , Draft draft ) {
27- super (uri , draft );
28- this .ta = ta ;
29- }
30-
31- public void onMessage (String message ) {
32- ta .append (message + "\n " );
33- }
34-
35- public void onOpen () {
36- ta .append ("You are connected to ChatServer: " + getURI () + "\n " );
37- }
38-
39- public void onClose () {
40- ta .append ("You have been disconnected from: " + getURI () + "\n " );
41- }
42-
43- public void onIOError (IOException ex ) {
44- ta .append ("Network problem ...\n " );
45- }
46-
47- /**
48- * The JFrame for our Chat client.
49- */
50- private static class Frame extends JFrame implements ActionListener {
51- private static final long serialVersionUID = -6056260699202978657L ;
52-
53- private final JTextField uriField ;
54- private final JButton connect ;
55- private final JButton close ;
56- private final JTextArea area ;
57- private final JTextField chatField ;
58- private ChatClient cc ;
59-
60- public Frame () {
61- super ("WebSocket Chat Client" );
62- Container c = getContentPane ();
63- GridLayout layout = new GridLayout ();
64- layout .setColumns (1 );
65- layout .setRows (5 );
66- c .setLayout (layout );
67-
68- uriField = new JTextField ();
69- uriField .setText ("ws://localhost:8887" );
70- c .add (uriField );
71-
72- connect = new JButton ("Connect" );
73- connect .addActionListener (this );
74- c .add (connect );
75-
76- close = new JButton ("Close" );
77- close .addActionListener (this );
78- c .add (close );
79-
80- JScrollPane scroll = new JScrollPane ();
81- area = new JTextArea ();
82- scroll .setViewportView (area );
83- c .add (scroll );
84-
85- chatField = new JTextField ();
86- chatField .setText ("" );
87- chatField .addActionListener (this );
88- c .add (chatField );
89-
90- java .awt .Dimension d = new java .awt .Dimension (300 , 400 );
91- setPreferredSize (d );
92- setSize (d );
93-
94- addWindowListener (new java .awt .event .WindowAdapter () {
95- @ Override
96- public void windowClosing (WindowEvent e ) {
97- if (cc != null ) {
98- try {
99- cc .close ();
100- } catch (IOException ex ) { ex .printStackTrace (); }
101- }
102- Frame .this .dispose ();
103- }
104- });
105-
106- setLocationRelativeTo (null );
107- setVisible (true );
108- }
109-
110- public void actionPerformed (ActionEvent e ) {
111-
112- if (e .getSource () == chatField ) {
113- if (cc != null ) {
114- try {
115- cc .send (chatField .getText ());
116- chatField .setText ("" );
117- chatField .requestFocus ();
118- } catch (IOException ex ) { ex .printStackTrace (); }
119- }
120-
121-
122- } else if (e .getSource () == connect ) {
123- connect .setEnabled (false );
124- uriField .setEditable (false );
125- try {
126- cc = new ChatClient (new URI (uriField .getText ()), area , new Draft_10 () );
127- cc .connect ();
128- } catch (URISyntaxException ex ) {
129- area .append (uriField .getText () + " is not a valid WebSocket URI\n " );
130- connect .setEnabled (true );
131- uriField .setEditable (true );
132- }
133- } else if (e .getSource () == close ) {
134- try {
135- cc .close ();
136- } catch (Exception ex ) {
137- ex .printStackTrace ();
138- }
139- }
140- }
141- }
142-
143- public static void main (String [] args ) {
144- java .awt .EventQueue .invokeLater (new Runnable () {
145- public void run () {
146- new Frame ();
147- }
148- });
149- }
150- }
21+ import net .tootallnate .websocket .drafts .Draft_75 ;
22+ import net .tootallnate .websocket .drafts .Draft_76 ;
23+
24+
25+ public class ChatClient extends JFrame implements ActionListener {
26+ private static final long serialVersionUID = -6056260699202978657L ;
27+
28+ private final JTextField uriField ;
29+ private final JButton connect ;
30+ private final JButton close ;
31+ private final JTextArea ta ;
32+ private final JTextField chatField ;
33+ private final JComboBox draft ;
34+ private WebSocketClient cc ;
35+
36+ public ChatClient () {
37+ super ("WebSocket Chat Client" );
38+ Container c = getContentPane ();
39+ GridLayout layout = new GridLayout ();
40+ layout .setColumns (1 );
41+ layout .setRows (6 );
42+ c .setLayout (layout );
43+
44+ Draft [] drafts = { new Draft_10 (), new Draft_76 (), new Draft_75 () };
45+ draft = new JComboBox ( drafts );
46+ c .add (draft );
47+
48+ uriField = new JTextField ();
49+ uriField .setText ("ws://localhost:8887" );
50+ c .add (uriField );
51+
52+ connect = new JButton ("Connect" );
53+ connect .addActionListener (this );
54+ c .add (connect );
55+
56+ close = new JButton ("Close" );
57+ close .addActionListener (this );
58+ close .setEnabled ( false );
59+ c .add (close );
60+
61+
62+ JScrollPane scroll = new JScrollPane ();
63+ ta = new JTextArea ();
64+ scroll .setViewportView (ta );
65+ c .add (scroll );
66+
67+ chatField = new JTextField ();
68+ chatField .setText ("" );
69+ chatField .addActionListener (this );
70+ c .add (chatField );
71+
72+ java .awt .Dimension d = new java .awt .Dimension (300 , 400 );
73+ setPreferredSize (d );
74+ setSize (d );
75+
76+ addWindowListener (new java .awt .event .WindowAdapter () {
77+ @ Override
78+ public void windowClosing (WindowEvent e ) {
79+ if (cc != null ) {
80+ try {
81+ cc .close ();
82+ } catch (IOException ex ) {
83+ ex .printStackTrace ();
84+ }
85+ }
86+ dispose ();
87+ }
88+ });
89+
90+ setLocationRelativeTo (null );
91+ setVisible (true );
92+ }
93+
94+ public void actionPerformed (ActionEvent e ) {
95+
96+ if (e .getSource () == chatField ) {
97+ if (cc != null ) {
98+ try {
99+ cc .send (chatField .getText ());
100+ chatField .setText ("" );
101+ chatField .requestFocus ();
102+ } catch (IOException ex ) { ex .printStackTrace (); }
103+ }
104+
105+ } else if (e .getSource () == connect ) {
106+ try {
107+ //cc = new ChatClient(new URI(uriField.getText()), area, ( Draft ) draft.getSelectedItem() );
108+ cc = new WebSocketClient ( new URI (uriField .getText ()),( Draft ) draft .getSelectedItem ()) {
109+
110+ public void onMessage (String message ) {
111+ ta .append ( "got: " + message + "\n " );
112+ ta .setCaretPosition ( ta .getDocument ().getLength () );
113+ }
114+
115+ public void onOpen () {
116+ ta .append ("You are connected to ChatServer: " + getURI () + "\n " );
117+ ta .setCaretPosition ( ta .getDocument ().getLength () );
118+ }
119+
120+ public void onClose () {
121+ ta .append ("You have been disconnected from: " + getURI () + "\n " );
122+ ta .setCaretPosition ( ta .getDocument ().getLength () );
123+ connect .setEnabled (true );
124+ uriField .setEditable (true );
125+ draft .setEditable (true );
126+ close .setEnabled ( false );
127+ }
128+
129+ public void onIOError (IOException ex ) {
130+ ta .append ("Network problem ...\n " +ex +"\n " );
131+ ta .setCaretPosition ( ta .getDocument ().getLength () );
132+ ex .printStackTrace ();
133+ connect .setEnabled (true );
134+ uriField .setEditable (true );
135+ draft .setEditable (true );
136+ close .setEnabled ( false );
137+ }
138+ };
139+
140+ close .setEnabled ( true );
141+ connect .setEnabled (false );
142+ uriField .setEditable (false );
143+ draft .setEditable (false );
144+ cc .connect ();
145+ } catch (URISyntaxException ex ) {
146+ ta .append (uriField .getText () + " is not a valid WebSocket URI\n " );
147+ }
148+ } else if (e .getSource () == close ) {
149+ try {
150+ cc .close ();
151+ } catch (Exception ex ) {
152+ ex .printStackTrace ();
153+ }
154+ }
155+ }
156+
157+ public static void main (String [] args ) {
158+ new ChatClient ();
159+ }
160+
161+ }
0 commit comments