|
45 | 45 | import org.java_websocket.handshake.ServerHandshake; |
46 | 46 |
|
47 | 47 | public class ChatClient extends JFrame implements ActionListener { |
48 | | - private static final long serialVersionUID = -6056260699202978657L; |
49 | | - |
50 | | - private final JTextField uriField; |
51 | | - private final JButton connect; |
52 | | - private final JButton close; |
53 | | - private final JTextArea ta; |
54 | | - private final JTextField chatField; |
55 | | - private final JComboBox draft; |
56 | | - private WebSocketClient cc; |
57 | | - |
58 | | - public ChatClient( String defaultlocation ) { |
59 | | - super( "WebSocket Chat Client" ); |
60 | | - Container c = getContentPane(); |
61 | | - GridLayout layout = new GridLayout(); |
62 | | - layout.setColumns( 1 ); |
63 | | - layout.setRows( 6 ); |
64 | | - c.setLayout( layout ); |
65 | | - |
66 | | - Draft[] drafts = { new Draft_6455() }; |
67 | | - draft = new JComboBox( drafts ); |
68 | | - c.add( draft ); |
69 | | - |
70 | | - uriField = new JTextField(); |
71 | | - uriField.setText( defaultlocation ); |
72 | | - c.add( uriField ); |
73 | | - |
74 | | - connect = new JButton( "Connect" ); |
75 | | - connect.addActionListener( this ); |
76 | | - c.add( connect ); |
77 | | - |
78 | | - close = new JButton( "Close" ); |
79 | | - close.addActionListener( this ); |
80 | | - close.setEnabled( false ); |
81 | | - c.add( close ); |
82 | | - |
83 | | - JScrollPane scroll = new JScrollPane(); |
84 | | - ta = new JTextArea(); |
85 | | - scroll.setViewportView( ta ); |
86 | | - c.add( scroll ); |
87 | | - |
88 | | - chatField = new JTextField(); |
89 | | - chatField.setText( "" ); |
90 | | - chatField.addActionListener( this ); |
91 | | - c.add( chatField ); |
92 | | - |
93 | | - java.awt.Dimension d = new java.awt.Dimension( 300, 400 ); |
94 | | - setPreferredSize( d ); |
95 | | - setSize( d ); |
96 | | - |
97 | | - addWindowListener( new java.awt.event.WindowAdapter() { |
98 | | - @Override |
99 | | - public void windowClosing( WindowEvent e ) { |
100 | | - if( cc != null ) { |
101 | | - cc.close(); |
102 | | - } |
103 | | - dispose(); |
104 | | - } |
105 | | - } ); |
106 | | - |
107 | | - setLocationRelativeTo( null ); |
108 | | - setVisible( true ); |
109 | | - } |
110 | | - |
111 | | - public void actionPerformed( ActionEvent e ) { |
112 | | - |
113 | | - if( e.getSource() == chatField ) { |
114 | | - if( cc != null ) { |
115 | | - cc.send( chatField.getText() ); |
116 | | - chatField.setText( "" ); |
117 | | - chatField.requestFocus(); |
118 | | - } |
119 | | - |
120 | | - } else if( e.getSource() == connect ) { |
121 | | - try { |
122 | | - // cc = new ChatClient(new URI(uriField.getText()), area, ( Draft ) draft.getSelectedItem() ); |
123 | | - cc = new WebSocketClient( new URI( uriField.getText() ), (Draft) draft.getSelectedItem() ) { |
124 | | - |
125 | | - @Override |
126 | | - public void onMessage( String message ) { |
127 | | - ta.append( "got: " + message + "\n" ); |
128 | | - ta.setCaretPosition( ta.getDocument().getLength() ); |
129 | | - } |
130 | | - |
131 | | - @Override |
132 | | - public void onOpen( ServerHandshake handshake ) { |
133 | | - ta.append( "You are connected to ChatServer: " + getURI() + "\n" ); |
134 | | - ta.setCaretPosition( ta.getDocument().getLength() ); |
135 | | - } |
136 | | - |
137 | | - @Override |
138 | | - public void onClose( int code, String reason, boolean remote ) { |
139 | | - ta.append( "You have been disconnected from: " + getURI() + "; Code: " + code + " " + reason + "\n" ); |
140 | | - ta.setCaretPosition( ta.getDocument().getLength() ); |
141 | | - connect.setEnabled( true ); |
142 | | - uriField.setEditable( true ); |
143 | | - draft.setEditable( true ); |
144 | | - close.setEnabled( false ); |
145 | | - } |
146 | | - |
147 | | - @Override |
148 | | - public void onError( Exception ex ) { |
149 | | - ta.append( "Exception occurred ...\n" + ex + "\n" ); |
150 | | - ta.setCaretPosition( ta.getDocument().getLength() ); |
151 | | - ex.printStackTrace(); |
152 | | - connect.setEnabled( true ); |
153 | | - uriField.setEditable( true ); |
154 | | - draft.setEditable( true ); |
155 | | - close.setEnabled( false ); |
156 | | - } |
157 | | - }; |
158 | | - |
159 | | - close.setEnabled( true ); |
160 | | - connect.setEnabled( false ); |
161 | | - uriField.setEditable( false ); |
162 | | - draft.setEditable( false ); |
163 | | - cc.connect(); |
164 | | - } catch ( URISyntaxException ex ) { |
165 | | - ta.append( uriField.getText() + " is not a valid WebSocket URI\n" ); |
166 | | - } |
167 | | - } else if( e.getSource() == close ) { |
168 | | - cc.close(); |
169 | | - } |
170 | | - } |
171 | | - |
172 | | - public static void main( String[] args ) { |
173 | | - String location; |
174 | | - if( args.length != 0 ) { |
175 | | - location = args[ 0 ]; |
176 | | - System.out.println( "Default server url specified: \'" + location + "\'" ); |
177 | | - } else { |
178 | | - location = "ws://localhost:8887"; |
179 | | - System.out.println( "Default server url not specified: defaulting to \'" + location + "\'" ); |
180 | | - } |
181 | | - new ChatClient( location ); |
182 | | - } |
| 48 | + |
| 49 | + private static final long serialVersionUID = -6056260699202978657L; |
| 50 | + |
| 51 | + private final JTextField uriField; |
| 52 | + private final JButton connect; |
| 53 | + private final JButton close; |
| 54 | + private final JTextArea ta; |
| 55 | + private final JTextField chatField; |
| 56 | + private final JComboBox draft; |
| 57 | + private WebSocketClient cc; |
| 58 | + |
| 59 | + public ChatClient(String defaultlocation) { |
| 60 | + super("WebSocket Chat Client"); |
| 61 | + Container c = getContentPane(); |
| 62 | + GridLayout layout = new GridLayout(); |
| 63 | + layout.setColumns(1); |
| 64 | + layout.setRows(6); |
| 65 | + c.setLayout(layout); |
| 66 | + |
| 67 | + Draft[] drafts = {new Draft_6455()}; |
| 68 | + draft = new JComboBox(drafts); |
| 69 | + c.add(draft); |
| 70 | + |
| 71 | + uriField = new JTextField(); |
| 72 | + uriField.setText(defaultlocation); |
| 73 | + c.add(uriField); |
| 74 | + |
| 75 | + connect = new JButton("Connect"); |
| 76 | + connect.addActionListener(this); |
| 77 | + c.add(connect); |
| 78 | + |
| 79 | + close = new JButton("Close"); |
| 80 | + close.addActionListener(this); |
| 81 | + close.setEnabled(false); |
| 82 | + c.add(close); |
| 83 | + |
| 84 | + JScrollPane scroll = new JScrollPane(); |
| 85 | + ta = new JTextArea(); |
| 86 | + scroll.setViewportView(ta); |
| 87 | + c.add(scroll); |
| 88 | + |
| 89 | + chatField = new JTextField(); |
| 90 | + chatField.setText(""); |
| 91 | + chatField.addActionListener(this); |
| 92 | + c.add(chatField); |
| 93 | + |
| 94 | + java.awt.Dimension d = new java.awt.Dimension(300, 400); |
| 95 | + setPreferredSize(d); |
| 96 | + setSize(d); |
| 97 | + |
| 98 | + addWindowListener(new java.awt.event.WindowAdapter() { |
| 99 | + @Override |
| 100 | + public void windowClosing(WindowEvent e) { |
| 101 | + if (cc != null) { |
| 102 | + cc.close(); |
| 103 | + } |
| 104 | + dispose(); |
| 105 | + } |
| 106 | + }); |
| 107 | + |
| 108 | + setLocationRelativeTo(null); |
| 109 | + setVisible(true); |
| 110 | + } |
| 111 | + |
| 112 | + public void actionPerformed(ActionEvent e) { |
| 113 | + |
| 114 | + if (e.getSource() == chatField) { |
| 115 | + if (cc != null) { |
| 116 | + cc.send(chatField.getText()); |
| 117 | + chatField.setText(""); |
| 118 | + chatField.requestFocus(); |
| 119 | + } |
| 120 | + |
| 121 | + } else if (e.getSource() == connect) { |
| 122 | + try { |
| 123 | + // cc = new ChatClient(new URI(uriField.getText()), area, ( Draft ) draft.getSelectedItem() ); |
| 124 | + cc = new WebSocketClient(new URI(uriField.getText()), (Draft) draft.getSelectedItem()) { |
| 125 | + |
| 126 | + @Override |
| 127 | + public void onMessage(String message) { |
| 128 | + ta.append("got: " + message + "\n"); |
| 129 | + ta.setCaretPosition(ta.getDocument().getLength()); |
| 130 | + } |
| 131 | + |
| 132 | + @Override |
| 133 | + public void onOpen(ServerHandshake handshake) { |
| 134 | + ta.append("You are connected to ChatServer: " + getURI() + "\n"); |
| 135 | + ta.setCaretPosition(ta.getDocument().getLength()); |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void onClose(int code, String reason, boolean remote) { |
| 140 | + ta.append( |
| 141 | + "You have been disconnected from: " + getURI() + "; Code: " + code + " " + reason |
| 142 | + + "\n"); |
| 143 | + ta.setCaretPosition(ta.getDocument().getLength()); |
| 144 | + connect.setEnabled(true); |
| 145 | + uriField.setEditable(true); |
| 146 | + draft.setEditable(true); |
| 147 | + close.setEnabled(false); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public void onError(Exception ex) { |
| 152 | + ta.append("Exception occurred ...\n" + ex + "\n"); |
| 153 | + ta.setCaretPosition(ta.getDocument().getLength()); |
| 154 | + ex.printStackTrace(); |
| 155 | + connect.setEnabled(true); |
| 156 | + uriField.setEditable(true); |
| 157 | + draft.setEditable(true); |
| 158 | + close.setEnabled(false); |
| 159 | + } |
| 160 | + }; |
| 161 | + |
| 162 | + close.setEnabled(true); |
| 163 | + connect.setEnabled(false); |
| 164 | + uriField.setEditable(false); |
| 165 | + draft.setEditable(false); |
| 166 | + cc.connect(); |
| 167 | + } catch (URISyntaxException ex) { |
| 168 | + ta.append(uriField.getText() + " is not a valid WebSocket URI\n"); |
| 169 | + } |
| 170 | + } else if (e.getSource() == close) { |
| 171 | + cc.close(); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + public static void main(String[] args) { |
| 176 | + String location; |
| 177 | + if (args.length != 0) { |
| 178 | + location = args[0]; |
| 179 | + System.out.println("Default server url specified: \'" + location + "\'"); |
| 180 | + } else { |
| 181 | + location = "ws://localhost:8887"; |
| 182 | + System.out.println("Default server url not specified: defaulting to \'" + location + "\'"); |
| 183 | + } |
| 184 | + new ChatClient(location); |
| 185 | + } |
183 | 186 |
|
184 | 187 | } |
0 commit comments