@@ -20,17 +20,26 @@ public class WebsocketServer {
20
20
private boolean isOauthEnabled = false ;
21
21
22
22
public WebsocketServer () {
23
- this (null );
23
+ this ((Javalin ) null );
24
+ }
25
+
26
+ public WebsocketServer (Javalin server ) {
27
+ this (server , null );
24
28
}
25
29
26
30
public WebsocketServer (WsExceptionHandler <Exception > exceptionHandler ) {
31
+ this (null , exceptionHandler );
32
+ }
33
+
34
+ public WebsocketServer (Javalin server , WsExceptionHandler <Exception > exceptionHandler ) {
27
35
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
+
32
40
if (exceptionHandler != null )
33
41
wss .wsException (Exception .class , exceptionHandler );
42
+
34
43
wss .wsException (Exception .class , (e , ctx ) -> {
35
44
log .error ("Uncaught websocket-exception in Websocket-Server: {}" , e );
36
45
});
@@ -40,11 +49,20 @@ public WebsocketServer(WsExceptionHandler<Exception> exceptionHandler) {
40
49
}
41
50
42
51
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 );
44
57
}
45
58
46
59
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 );
48
66
if (keycloakHost == null || keycloakHost .isEmpty ()) {
49
67
throw new IllegalArgumentException ("Keycloak host must not be null or empty." );
50
68
}
@@ -64,6 +82,14 @@ public WebsocketServer(String keycloakHost, String keycloakRealm, WsExceptionHan
64
82
}
65
83
}
66
84
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
+ */
67
93
public WebsocketServer start (int port ) {
68
94
wss .start ("0.0.0.0" , port );
69
95
log .debug ("Websocket server started on port: {}" , port );
0 commit comments