public void stop ( int timeout ) throws InterruptedException { if ( ! isclosed . compareAndSet ( false , true ) ) { // this also makes sure that no further connections will be added to this.connections return ; } List < WebSocket > socketsToClose ; // copy the connections in a list (prevent callback deadlocks) synchronized ( connections ) { socketsToClose = new ArrayList < WebSocket > ( connections ) ; } for ( WebSocket ws : socketsToClose ) { ws . close ( CloseFrame . GOING_AWAY ) ; } wsf . close ( ) ; synchronized ( this ) { if ( selectorthread != null && selector != null ) { selector . wakeup ( ) ; selectorthread . join ( timeout ) ; } } }
Closes all connected clients sockets then closes the underlying ServerSocketChannel effectively killing the server socket selectorthread freeing the port the server was bound to and stops all internal workerthreads .