11
11
12
12
import org .glassfish .tyrus .client .ClientManager ;
13
13
14
+ import info .unterrainer .commons .serialization .jsonmapper .JsonMapper ;
14
15
import info .unterrainer .oauthtokenmanager .LocalOauthTokens ;
15
16
import info .unterrainer .oauthtokenmanager .OauthTokenManager ;
16
17
import info .unterrainer .websocketclient .exceptions .WebsocketClosingException ;
@@ -45,6 +46,7 @@ public class WebsocketConnection implements AutoCloseable {
45
46
final String keycloakPassword ;
46
47
47
48
private WebsocketEndpoints endpoints ;
49
+ private JsonMapper jsonMapper = JsonMapper .create ();
48
50
49
51
final CompletableFuture <Session > sessionReady = new CompletableFuture <>();
50
52
@@ -71,6 +73,28 @@ public void send(String message) {
71
73
log .debug ("Sent message: " + message );
72
74
}
73
75
76
+ public void send (byte [] message ) {
77
+ Session s = awaitOpen (Duration .ofMillis (5000L ));
78
+ try {
79
+ s .getBasicRemote ().sendBinary (java .nio .ByteBuffer .wrap (message ));
80
+ } catch (Exception e ) {
81
+ log .error ("Error sending binary message: " , e );
82
+ throw new WebsocketSendingMessageException ("Failed to send binary message." , e );
83
+ }
84
+ log .debug ("Sent binary message of length: " + message .length );
85
+ }
86
+
87
+ public void <T > send (T message ) {
88
+ Session s = awaitOpen (Duration .ofMillis (5000L ));
89
+ try {
90
+ s .getBasicRemote ().sendText (jsonMapper .toStringFrom (message ));
91
+ } catch (Exception e ) {
92
+ log .error ("Error sending message: " , e );
93
+ throw new WebsocketSendingMessageException (String .format ("Failed to send message [%s]." , message ), e );
94
+ }
95
+ log .debug ("Sent message: " + message );
96
+ }
97
+
74
98
@ Override
75
99
public void close () {
76
100
Session s = awaitOpen (Duration .ofMillis (5000L ));
0 commit comments