@@ -136,7 +136,11 @@ public void await() throws InterruptedException {
136
136
public void onClose (int statusCode , String reason ) {
137
137
System .out .printf ("Connection closed: %d - %s%n" , statusCode , reason );
138
138
this .closeLatch .countDown (); // trigger latch
139
- this .onDisconnect .run (null );
139
+ try {
140
+ this .onDisconnect .run (null );
141
+ } catch (Throwable t ) {
142
+ t .printStackTrace ();
143
+ }
140
144
}
141
145
142
146
@ OnWebSocketConnect
@@ -169,98 +173,125 @@ public void onMessage(String msg) {
169
173
Class type = messageTypes .get (responseBase .getMessageId ());
170
174
responseBase = (ResponseBase ) new Gson ().fromJson (msg , type );
171
175
172
- switch (type .getSimpleName ()) {
173
- case "GetVersionResponse" :
174
- versionInfo = (GetVersionResponse ) responseBase ;
175
- System .out .printf ("Connected to OBS. Websocket Version: %s, Studio Version: %s\n " , versionInfo .getObsWebsocketVersion (), versionInfo .getObsStudioVersion ());
176
- session .getRemote ().sendStringByFuture (new Gson ().toJson (new GetAuthRequiredRequest (this )));
177
- break ;
178
- case "GetAuthRequiredResponse" :
179
- GetAuthRequiredResponse authRequiredResponse = (GetAuthRequiredResponse ) responseBase ;
180
- if (authRequiredResponse .isAuthRequired ()) {
181
- System .out .println ("Authentication is required." );
182
- authenticateWithServer (authRequiredResponse .getChallenge (), authRequiredResponse .getSalt ());
183
- } else {
184
- System .out .println ("Authentication is not required. You're ready to go!" );
185
- this .onConnect .run (versionInfo );
186
- }
187
- break ;
188
-
189
- case "AuthenticateResponse" :
190
- AuthenticateResponse authenticateResponse = (AuthenticateResponse ) responseBase ;
191
-
192
- if ("ok" .equals (authenticateResponse .getStatus ())) {
193
- this .onConnect .run (versionInfo );
194
- } else {
195
- this .onConnectionFailed .run ("Failed to authenticate with password. Error: " + authenticateResponse .getError ());
196
- }
197
-
198
- break ;
199
- default :
200
- if (callbacks .containsKey (type )) {
201
- callbacks .get (type ).run (responseBase );
202
- } else {
203
- System .out .println ("Invalid type received: " + type .getName ());
204
- }
176
+ try {
177
+ processIncomingResponse (responseBase , type );
178
+ } catch (Throwable t ) {
179
+ System .err .println ("Failed to process response '" + type .getSimpleName () + "' from websocket." );
180
+ t .printStackTrace ();
205
181
}
182
+
206
183
} else {
207
184
JsonElement elem = new JsonParser ().parse (msg );
208
185
EventType eventType ;
209
186
210
187
try {
211
188
eventType = EventType .valueOf (elem .getAsJsonObject ().get ("update-type" ).getAsString ());
212
- } catch (Exception e ) {
189
+ } catch (Throwable t ) {
213
190
return ;
214
191
}
215
192
216
- switch (eventType ) {
217
- case ReplayStarted :
218
- if (onReplayStarted != null )
219
- onReplayStarted .run (null );
220
- break ;
221
- case ReplayStarting :
222
- if (onReplayStarting != null )
223
- onReplayStarting .run (null );
224
- break ;
225
- case ReplayStopped :
226
- if (onReplayStopped != null )
227
- onReplayStopped .run (null );
228
- break ;
229
- case ReplayStopping :
230
- if (onReplayStopping != null )
231
- onReplayStopping .run (null );
232
- break ;
233
- case SwitchScenes :
234
- if (onSwitchScenes != null ) {
235
- onSwitchScenes .run (new Gson ().fromJson (msg , SwitchScenesResponse .class ));
236
- }
237
- break ;
238
- case ScenesChanged :
239
- if (onScenesChanged != null ) {
240
- onScenesChanged .run (new Gson ().fromJson (msg , ScenesChangedResponse .class ));
241
- }
242
- break ;
243
- case TransitionBegin :
244
- if (onTransitionBegin != null ) {
245
- onTransitionBegin .run (new Gson ().fromJson (msg , TransitionBeginResponse .class ));
246
- }
247
- break ;
248
- case TransitionEnd :
249
- if (onTransitionEnd != null ) {
250
- onTransitionEnd .run (new Gson ().fromJson (msg , TransitionEndResponse .class ));
251
- }
252
- break ;
193
+ try {
194
+ processIncomingEvent (msg , eventType );
195
+ } catch (Throwable t ) {
196
+ System .err .println ("Failed to execute callback for event: " + eventType );
197
+ t .printStackTrace ();
253
198
}
254
199
}
255
- } catch (Exception e ) {
256
- System .out .println ("Websocket Exception: " + e .getMessage ());
200
+ } catch (Throwable t ) {
201
+ System .err .println ("Failed to process message from websocket." );
202
+ t .printStackTrace ();
203
+ }
204
+ }
205
+
206
+ private void processIncomingResponse (ResponseBase responseBase , Class type ) {
207
+ switch (type .getSimpleName ()) {
208
+ case "GetVersionResponse" :
209
+ versionInfo = (GetVersionResponse ) responseBase ;
210
+ System .out .printf ("Connected to OBS. Websocket Version: %s, Studio Version: %s\n " , versionInfo .getObsWebsocketVersion (), versionInfo .getObsStudioVersion ());
211
+ session .getRemote ().sendStringByFuture (new Gson ().toJson (new GetAuthRequiredRequest (this )));
212
+ break ;
213
+
214
+ case "GetAuthRequiredResponse" :
215
+ GetAuthRequiredResponse authRequiredResponse = (GetAuthRequiredResponse ) responseBase ;
216
+ if (authRequiredResponse .isAuthRequired ()) {
217
+ System .out .println ("Authentication is required." );
218
+ authenticateWithServer (authRequiredResponse .getChallenge (), authRequiredResponse .getSalt ());
219
+ } else {
220
+ System .out .println ("Authentication is not required. You're ready to go!" );
221
+ runOnConnect (versionInfo );
222
+ }
223
+ break ;
224
+
225
+ case "AuthenticateResponse" :
226
+ AuthenticateResponse authenticateResponse = (AuthenticateResponse ) responseBase ;
227
+
228
+ if ("ok" .equals (authenticateResponse .getStatus ())) {
229
+ runOnConnect (versionInfo );
230
+ } else {
231
+ runOnConnectionFailed ("Failed to authenticate with password. Error: " + authenticateResponse .getError ());
232
+ }
233
+
234
+ break ;
235
+ default :
236
+ if (!callbacks .containsKey (type )) {
237
+ System .out .println ("Invalid type received: " + type .getName ());
238
+ return ;
239
+ }
240
+
241
+ try {
242
+ callbacks .get (type ).run (responseBase );
243
+ } catch (Throwable t ) {
244
+ System .err .println ("Failed to execute callback for response: " + type );
245
+ t .printStackTrace ();
246
+ }
247
+ }
248
+ }
249
+
250
+ private void processIncomingEvent (String msg , EventType eventType ) {
251
+ switch (eventType ) {
252
+ case ReplayStarted :
253
+ if (onReplayStarted != null )
254
+ onReplayStarted .run (null );
255
+ break ;
256
+ case ReplayStarting :
257
+ if (onReplayStarting != null )
258
+ onReplayStarting .run (null );
259
+ break ;
260
+ case ReplayStopped :
261
+ if (onReplayStopped != null )
262
+ onReplayStopped .run (null );
263
+ break ;
264
+ case ReplayStopping :
265
+ if (onReplayStopping != null )
266
+ onReplayStopping .run (null );
267
+ break ;
268
+ case SwitchScenes :
269
+ if (onSwitchScenes != null ) {
270
+ onSwitchScenes .run (new Gson ().fromJson (msg , SwitchScenesResponse .class ));
271
+ }
272
+ break ;
273
+ case ScenesChanged :
274
+ if (onScenesChanged != null ) {
275
+ onScenesChanged .run (new Gson ().fromJson (msg , ScenesChangedResponse .class ));
276
+ }
277
+ break ;
278
+ case TransitionBegin :
279
+ if (onTransitionBegin != null ) {
280
+ onTransitionBegin .run (new Gson ().fromJson (msg , TransitionBeginResponse .class ));
281
+ }
282
+ break ;
283
+ case TransitionEnd :
284
+ if (onTransitionEnd != null ) {
285
+ onTransitionEnd .run (new Gson ().fromJson (msg , TransitionEndResponse .class ));
286
+ }
287
+ break ;
257
288
}
258
289
}
259
290
260
291
private void authenticateWithServer (String challenge , String salt ) {
261
292
if (password == null ) {
262
- System .err .println ("Authentication required by server but no password set for client" );
263
- this . onConnectionFailed . run ("Authentication required by server but no password set for client" );
293
+ System .err .println ("Authentication required by server but no password set by client" );
294
+ runOnConnectionFailed ("Authentication required by server but no password set by client" );
264
295
return ;
265
296
}
266
297
@@ -270,7 +301,7 @@ private void authenticateWithServer(String challenge, String salt) {
270
301
} catch (NoSuchAlgorithmException e ) {
271
302
System .err .println ("Failed to perform password authentication with server" );
272
303
e .printStackTrace ();
273
- this . onConnectionFailed . run ("Failed to perform password authentication with server" );
304
+ runOnConnectionFailed ("Failed to perform password authentication with server" );
274
305
return ;
275
306
}
276
307
@@ -530,4 +561,30 @@ public void setStudioModeEnabled(boolean enabled, Callback callback){
530
561
session .getRemote ().sendStringByFuture (new Gson ().toJson (request ));
531
562
callbacks .put (SetStudioModeEnabledResponse .class , callback );
532
563
}
564
+
565
+ private void runOnConnectionFailed (String message ) {
566
+ if (onConnectionFailed == null ) {
567
+ return ;
568
+ }
569
+
570
+ try {
571
+ onConnectionFailed .run (message );
572
+ } catch (Throwable t ) {
573
+ System .err .println ("Exception during callback execution for 'onConnectionFailed'" );
574
+ t .printStackTrace ();
575
+ }
576
+ }
577
+
578
+ private void runOnConnect (GetVersionResponse versionInfo ) {
579
+ if (onConnect == null ) {
580
+ return ;
581
+ }
582
+
583
+ try {
584
+ onConnect .run (versionInfo );
585
+ } catch (Throwable t ) {
586
+ System .err .println ("Exception during callback execution for 'onConnect'" );
587
+ t .printStackTrace ();
588
+ }
589
+ }
533
590
}
0 commit comments