88import java .nio .channels .SelectionKey ;
99import java .nio .channels .Selector ;
1010import java .nio .channels .SocketChannel ;
11- import java .nio .charset . CharacterCodingException ;
11+ import java .nio .channels . UnresolvedAddressException ;
1212import java .util .Iterator ;
1313import java .util .Set ;
1414
@@ -127,27 +127,33 @@ private void tryToConnect( InetSocketAddress remote ) throws IOException {
127127 client .connect ( remote );
128128 selector = Selector .open ();
129129 client .register ( selector , SelectionKey .OP_CONNECT );
130- conn = new WebSocket ( this , draft , client );
131130 }
132131
133132 // Runnable IMPLEMENTATION /////////////////////////////////////////////////
134133 public void run () {
135134 if ( thread == null )
136135 thread = Thread .currentThread ();
136+ interruptableRun ();
137+ thread = null ;
138+ }
139+
140+ protected void interruptableRun () {
137141 try {
138142 tryToConnect ( new InetSocketAddress ( uri .getHost (), getPort () ) );
139143 } catch ( ClosedByInterruptException e ) {
140144 onError ( null , e );
141145 return ;
142146 } catch ( IOException e ) {//
143147 onError ( conn , e );
144- conn .closeConnection ( CloseFrame .ABNROMAL_CLOSE );
145148 return ;
146149 } catch ( SecurityException e ) {
147150 onError ( conn , e );
148- conn .closeConnection ( CloseFrame .ABNROMAL_CLOSE );
151+ return ;
152+ } catch ( UnresolvedAddressException e ) {
153+ onError ( conn , e );
149154 return ;
150155 }
156+ conn = new WebSocket ( this , draft , client );
151157 try /*IO*/ {
152158 while ( !Thread .interrupted () && !conn .isClosed () ) {
153159 SelectionKey key = null ;
@@ -250,6 +256,7 @@ private void sendHandshake() throws IOException , InvalidHandshakeException , In
250256 * @param conn
251257 * @param message
252258 */
259+ @ Override
253260 public void onMessage ( WebSocket conn , String message ) {
254261 onMessage ( message );
255262 }
@@ -259,6 +266,7 @@ public void onMessage( WebSocket conn, String message ) {
259266 *
260267 * @param conn
261268 */
269+ @ Override
262270 public void onOpen ( WebSocket conn ) {
263271 onOpen ();
264272 }
@@ -268,6 +276,7 @@ public void onOpen( WebSocket conn ) {
268276 *
269277 * @param conn
270278 */
279+ @ Override
271280 public void onClose ( WebSocket conn ) {
272281 thread .interrupt ();
273282 onClose ();
@@ -278,7 +287,8 @@ public void onClose( WebSocket conn ) {
278287 *
279288 * @param conn
280289 */
281- public void onError ( WebSocket conn , IOException ex ) {
290+ @ Override
291+ public void onError ( WebSocket conn , Exception ex ) {
282292 onError ( ex );
283293 }
284294
0 commit comments