@@ -26,18 +26,24 @@ public static void onMessageString(Consumer<String> handler)
2626 {
2727 BoringParts theBoringParts = new BoringParts (handler ::accept );
2828
29- while (theBoringParts .isNotClosed ())
30- {
31- Thread .yield ();
32- try {
33- Thread .sleep (1000 );
34- } catch (InterruptedException e ) {}
29+ runThread (theBoringParts );
30+
31+ }).start ();
32+ }
33+
34+ /**
35+ * @param handler A {@link Consumer<String>} that we'll
36+ * run in a Thread that stays alive as long
37+ * as the WebSocket stays open
38+ * @param serverURI Address of the server to which a WebSocket
39+ * connection is established
40+ */
41+ public static void onMessageStringAlternativeServer (Consumer <String > handler , String serverURI ){
42+ new Thread (() -> {
43+ BoringParts theBoringParts = new BoringParts (handler ::accept , 0 , serverURI );
44+
45+ runThread (theBoringParts );
3546
36- if (Thread .interrupted ())
37- {
38- break ;
39- }
40- }
4147 }).start ();
4248 }
4349
@@ -80,4 +86,60 @@ public static void onMessage(CertStreamMessageHandler handler)
8086 handler .onMessage (fullMsg );
8187 });
8288 }
89+
90+ /**
91+ * @param handler A {@link Consumer<CertStreamMessage>} that we'll
92+ * run in a Thread that stays alive as long
93+ * as the WebSocket stays open
94+ * @param serverURI Address of the server to which a WebSocket
95+ * connection is established
96+ */
97+ public static void onMessageAlternativeServer (CertStreamMessageHandler handler , String serverURI ){
98+ onMessageStringAlternativeServer (string -> {
99+ CertStreamMessagePOJO msg ;
100+
101+ try {
102+ msg = new Gson ().fromJson (string , CertStreamMessagePOJO .class );
103+
104+ if (msg .messageType .equalsIgnoreCase ("heartbeat" )) {
105+ return ;
106+ }
107+ } catch (JsonSyntaxException e ) {
108+ System .out .println (e .getMessage ());
109+ logger .warn ("onMessageAlternativeServer had an exception parsing some json" , e );
110+ return ;
111+ }
112+
113+ CertStreamMessage fullMsg ;
114+
115+ try {
116+ fullMsg = CertStreamMessage .fromPOJO (msg );
117+ } catch (CertificateException e ){
118+ logger .warn ("Encountered a CertificateException" , e );
119+ return ;
120+ }
121+
122+ handler .onMessage (fullMsg );
123+ }, serverURI );
124+ }
125+
126+ /**
127+ * Runs thread until WebSocket is closed
128+ * @param theBoringParts Websocket connection that is checked
129+ * to see if it has been closed
130+ */
131+ public static void runThread (BoringParts theBoringParts ) {
132+ while (theBoringParts .isNotClosed ())
133+ {
134+ Thread .yield ();
135+ try {
136+ Thread .sleep (1000 );
137+ } catch (InterruptedException e ) {}
138+
139+ if (Thread .interrupted ())
140+ {
141+ break ;
142+ }
143+ }
144+ }
83145}
0 commit comments