File tree Expand file tree Collapse file tree 1 file changed +24
-9
lines changed
src/net/tootallnate/websocket Expand file tree Collapse file tree 1 file changed +24
-9
lines changed Original file line number Diff line number Diff line change @@ -120,8 +120,22 @@ public void close() throws IOException
120
120
{
121
121
if (running )
122
122
{
123
- selector .wakeup ();
124
- conn .close ();
123
+ // must be called to stop do loop
124
+ running = false ;
125
+
126
+ // call this inside IF because it can be null if the connection has't started
127
+ // but user is calling close()
128
+ if (selector != null && conn != null )
129
+ {
130
+ selector .wakeup ();
131
+ conn .close ();
132
+ // close() is synchronously calling onClose(conn) so we don't have to
133
+ }
134
+ else
135
+ {
136
+ // connection has't started but the onClose events should be triggered
137
+ onClose (conn );
138
+ }
125
139
}
126
140
}
127
141
@@ -164,8 +178,13 @@ private boolean tryToConnect(InetSocketAddress remote) {
164
178
selector = Selector .open ();
165
179
166
180
this .conn = new WebSocket (client , new LinkedBlockingQueue <ByteBuffer >(), this );
167
- // At first, we're only interested in the 'CONNECT' keys.
168
- client .register (selector , SelectionKey .OP_CONNECT );
181
+ // the client/selector can be null when closing the connection before its start
182
+ // so we have to call this part inside IF
183
+ if (client != null )
184
+ {
185
+ // At first, we're only interested in the 'CONNECT' keys.
186
+ client .register (selector , SelectionKey .OP_CONNECT );
187
+ }
169
188
170
189
} catch (IOException ex ) {
171
190
onIOError (conn , ex );
@@ -364,11 +383,7 @@ public void onOpen(WebSocket conn) {
364
383
*/
365
384
public void onClose (WebSocket conn )
366
385
{
367
- if (running )
368
- {
369
- onClose ();
370
- }
371
-
386
+ onClose ();
372
387
releaseAndInitialize ();
373
388
}
374
389
You can’t perform that action at this time.
0 commit comments