@@ -248,6 +248,10 @@ public void start() {
248248 new Thread (this ).start ();
249249 }
250250
251+ public void stop (int timeout ) throws InterruptedException {
252+ stop (timeout , "" );
253+ }
254+
251255 /**
252256 * Closes all connected clients sockets, then closes the underlying ServerSocketChannel,
253257 * effectively killing the server socket selectorthread, freeing the port the server was bound to
@@ -257,10 +261,11 @@ public void start() {
257261 *
258262 * @param timeout Specifies how many milliseconds the overall close handshaking may take
259263 * altogether before the connections are closed without proper close
260- * handshaking.<br>
264+ * handshaking.
265+ * @param closeMessage Specifies message for remote client<br>
261266 * @throws InterruptedException Interrupt
262267 */
263- public void stop (int timeout ) throws InterruptedException {
268+ public void stop (int timeout , String closeMessage ) throws InterruptedException {
264269 if (!isclosed .compareAndSet (false ,
265270 true )) { // this also makes sure that no further connections will be added to this.connections
266271 return ;
@@ -274,7 +279,7 @@ public void stop(int timeout) throws InterruptedException {
274279 }
275280
276281 for (WebSocket ws : socketsToClose ) {
277- ws .close (CloseFrame .GOING_AWAY );
282+ ws .close (CloseFrame .GOING_AWAY , closeMessage );
278283 }
279284
280285 wsf .close ();
@@ -680,6 +685,17 @@ private void handleIOException(SelectionKey key, WebSocket conn, IOException ex)
680685 private void handleFatal (WebSocket conn , Exception e ) {
681686 log .error ("Shutdown due to fatal error" , e );
682687 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+
683699 //Shutting down WebSocketWorkers, see #222
684700 if (decoders != null ) {
685701 for (WebSocketWorker w : decoders ) {
@@ -689,13 +705,6 @@ private void handleFatal(WebSocket conn, Exception e) {
689705 if (selectorthread != null ) {
690706 selectorthread .interrupt ();
691707 }
692- try {
693- stop ();
694- } catch (InterruptedException e1 ) {
695- Thread .currentThread ().interrupt ();
696- log .error ("Interrupt during stop" , e );
697- onError (null , e1 );
698- }
699708 }
700709
701710 @ Override
@@ -1080,9 +1089,6 @@ public void run() {
10801089 } catch (InterruptedException e ) {
10811090 Thread .currentThread ().interrupt ();
10821091 } catch (VirtualMachineError | ThreadDeath | LinkageError e ) {
1083- if (ws != null ) {
1084- ws .close ();
1085- }
10861092 log .error ("Got fatal error in worker thread {}" , getName ());
10871093 Exception exception = new Exception (e );
10881094 handleFatal (ws , exception );
0 commit comments