1111
1212import org .glassfish .tyrus .client .ClientManager ;
1313
14+ import info .unterrainer .commons .serialization .jsonmapper .JsonMapper ;
1415import info .unterrainer .oauthtokenmanager .LocalOauthTokens ;
1516import info .unterrainer .oauthtokenmanager .OauthTokenManager ;
1617import info .unterrainer .websocketclient .exceptions .WebsocketClosingException ;
@@ -45,6 +46,7 @@ public class WebsocketConnection implements AutoCloseable {
4546 final String keycloakPassword ;
4647
4748 private WebsocketEndpoints endpoints ;
49+ private JsonMapper jsonMapper = JsonMapper .create ();
4850
4951 final CompletableFuture <Session > sessionReady = new CompletableFuture <>();
5052
@@ -71,6 +73,28 @@ public void send(String message) {
7173 log .debug ("Sent message: " + message );
7274 }
7375
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+
7498 @ Override
7599 public void close () {
76100 Session s = awaitOpen (Duration .ofMillis (5000L ));
0 commit comments