@@ -248,6 +248,10 @@ public void start() {
248
248
new Thread (this ).start ();
249
249
}
250
250
251
+ public void stop (int timeout ) throws InterruptedException {
252
+ stop (timeout , "" );
253
+ }
254
+
251
255
/**
252
256
* Closes all connected clients sockets, then closes the underlying ServerSocketChannel,
253
257
* effectively killing the server socket selectorthread, freeing the port the server was bound to
@@ -257,10 +261,11 @@ public void start() {
257
261
*
258
262
* @param timeout Specifies how many milliseconds the overall close handshaking may take
259
263
* altogether before the connections are closed without proper close
260
- * handshaking.<br>
264
+ * handshaking.
265
+ * @param closeMessage Specifies message for remote client<br>
261
266
* @throws InterruptedException Interrupt
262
267
*/
263
- public void stop (int timeout ) throws InterruptedException {
268
+ public void stop (int timeout , String closeMessage ) throws InterruptedException {
264
269
if (!isclosed .compareAndSet (false ,
265
270
true )) { // this also makes sure that no further connections will be added to this.connections
266
271
return ;
@@ -274,7 +279,7 @@ public void stop(int timeout) throws InterruptedException {
274
279
}
275
280
276
281
for (WebSocket ws : socketsToClose ) {
277
- ws .close (CloseFrame .GOING_AWAY );
282
+ ws .close (CloseFrame .GOING_AWAY , closeMessage );
278
283
}
279
284
280
285
wsf .close ();
@@ -680,6 +685,17 @@ private void handleIOException(SelectionKey key, WebSocket conn, IOException ex)
680
685
private void handleFatal (WebSocket conn , Exception e ) {
681
686
log .error ("Shutdown due to fatal error" , e );
682
687
onError (conn , e );
688
+
689
+ String causeMessage = e .getCause () != null ? " caused by " + e .getCause ().getClass ().getName () : "" ;
690
+ String errorMessage = "Got error on server side: " + e .getClass ().getName () + causeMessage ;
691
+ try {
692
+ stop (0 , errorMessage );
693
+ } catch (InterruptedException e1 ) {
694
+ Thread .currentThread ().interrupt ();
695
+ log .error ("Interrupt during stop" , e );
696
+ onError (null , e1 );
697
+ }
698
+
683
699
//Shutting down WebSocketWorkers, see #222
684
700
if (decoders != null ) {
685
701
for (WebSocketWorker w : decoders ) {
@@ -689,13 +705,6 @@ private void handleFatal(WebSocket conn, Exception e) {
689
705
if (selectorthread != null ) {
690
706
selectorthread .interrupt ();
691
707
}
692
- try {
693
- stop ();
694
- } catch (InterruptedException e1 ) {
695
- Thread .currentThread ().interrupt ();
696
- log .error ("Interrupt during stop" , e );
697
- onError (null , e1 );
698
- }
699
708
}
700
709
701
710
@ Override
0 commit comments