@@ -20,17 +20,26 @@ public class WebsocketServer {
2020 private boolean isOauthEnabled = false ;
2121
2222 public WebsocketServer () {
23- this (null );
23+ this ((Javalin ) null );
24+ }
25+
26+ public WebsocketServer (Javalin server ) {
27+ this (server , null );
2428 }
2529
2630 public WebsocketServer (WsExceptionHandler <Exception > exceptionHandler ) {
31+ this (null , exceptionHandler );
32+ }
33+
34+ public WebsocketServer (Javalin server , WsExceptionHandler <Exception > exceptionHandler ) {
2735 try {
28- wss = Javalin . create () ;
29- wss . exception ( Exception . class , ( e , ctx ) -> {
30- log . error ( "Uncaught exception in Websocket-Server: {}" , e );
31- });
36+ wss = server ;
37+ if ( wss == null )
38+ wss = Javalin . create ( );
39+
3240 if (exceptionHandler != null )
3341 wss .wsException (Exception .class , exceptionHandler );
42+
3443 wss .wsException (Exception .class , (e , ctx ) -> {
3544 log .error ("Uncaught websocket-exception in Websocket-Server: {}" , e );
3645 });
@@ -40,11 +49,20 @@ public WebsocketServer(WsExceptionHandler<Exception> exceptionHandler) {
4049 }
4150
4251 public WebsocketServer (String keycloakHost , String keycloakRealm ) {
43- this (keycloakHost , keycloakRealm , null );
52+ this (null , keycloakHost , keycloakRealm );
53+ }
54+
55+ public WebsocketServer (Javalin server , String keycloakHost , String keycloakRealm ) {
56+ this (server , keycloakHost , keycloakRealm , null );
4457 }
4558
4659 public WebsocketServer (String keycloakHost , String keycloakRealm , WsExceptionHandler <Exception > exceptionHandler ) {
47- this (exceptionHandler );
60+ this (null , keycloakHost , keycloakRealm , exceptionHandler );
61+ }
62+
63+ public WebsocketServer (Javalin server , String keycloakHost , String keycloakRealm ,
64+ WsExceptionHandler <Exception > exceptionHandler ) {
65+ this (server , exceptionHandler );
4866 if (keycloakHost == null || keycloakHost .isEmpty ()) {
4967 throw new IllegalArgumentException ("Keycloak host must not be null or empty." );
5068 }
@@ -64,6 +82,14 @@ public WebsocketServer(String keycloakHost, String keycloakRealm, WsExceptionHan
6482 }
6583 }
6684
85+ /**
86+ * Starts the Websocket server on the specified port. Don't start this, if you
87+ * used this class as a decorator for an existing Javalin instance. Call the
88+ * other start method instead.
89+ *
90+ * @param port the port to listen to
91+ * @return
92+ */
6793 public WebsocketServer start (int port ) {
6894 wss .start ("0.0.0.0" , port );
6995 log .debug ("Websocket server started on port: {}" , port );
0 commit comments