1
1
package net .twasi .obsremotejava ;
2
2
3
- import net .twasi .obsremotejava .requests .ResponseBase ;
3
+ import net .twasi .obsremotejava .callbacks .Callback ;
4
+ import net .twasi .obsremotejava .callbacks .ErrorCallback ;
5
+ import net .twasi .obsremotejava .callbacks .StringCallback ;
6
+ import net .twasi .obsremotejava .objects .throwables .OBSResponseError ;
4
7
import org .eclipse .jetty .websocket .api .Session ;
5
8
import org .eclipse .jetty .websocket .client .ClientUpgradeRequest ;
6
9
import org .eclipse .jetty .websocket .client .WebSocketClient ;
10
13
import java .util .Map ;
11
14
import java .util .concurrent .ExecutionException ;
12
15
import java .util .concurrent .Future ;
16
+ import java .util .concurrent .TimeUnit ;
13
17
14
18
public class OBSRemoteController {
15
19
private String address ;
16
- private OBSCommunicator communicator ;
17
- private WebSocketClient client ;
20
+ private final boolean debug ;
21
+ private final OBSCommunicator communicator ;
22
+ private final String password ;
23
+ private final WebSocketClient client ;
24
+
25
+ private StringCallback onConnectionFailed ;
26
+ private ErrorCallback onError ;
18
27
19
28
private boolean failed ;
20
29
21
- public OBSRemoteController (String address , boolean debug , String password ) {
30
+ public OBSRemoteController (String address , boolean debug , String password , boolean autoConnect ) {
22
31
this .address = address ;
23
- communicator = new OBSCommunicator (debug , password );
32
+ this .debug = debug ;
33
+ this .password = password ;
34
+
24
35
client = new WebSocketClient ();
36
+ communicator = new OBSCommunicator (debug , password );
37
+
38
+ if (autoConnect ) {
39
+ connect ();
40
+ }
41
+ }
42
+
43
+ public OBSRemoteController (String address , boolean debug , String password ) {
44
+ this (address , debug , password , true );
45
+ }
46
+
47
+ public OBSRemoteController (String address , boolean debug ) {
48
+ this (address , debug , null );
49
+ }
50
+
51
+ public void connect () {
25
52
try {
26
53
client .start ();
54
+ } catch (Exception e ) {
55
+ System .err .println ("Failed to start WebSocketClient." );
56
+ e .printStackTrace ();
57
+ runOnError ("Failed to start WebSocketClient" , e );
58
+ return ;
59
+ }
27
60
61
+ try {
28
62
URI uri = new URI (address );
29
63
ClientUpgradeRequest request = new ClientUpgradeRequest ();
30
64
Future <Session > connection = client .connect (communicator , uri , request );
31
- System .out .printf ("Connecting to: %s%s%n" , uri , (password != null ? " with password" : "" ));
65
+ System .out .printf ("Connecting to: %s%s. %n" , uri , (password != null ? " with password" : "" ));
32
66
33
67
try {
34
68
connection .get ();
35
69
failed = false ;
36
70
} catch (ExecutionException e ) {
37
71
if (e .getCause () instanceof ConnectException ) {
38
- System .out .println ("Connection to OBS failed." );
72
+ System .out .println ("Failed to connect to OBS." );
73
+ e .printStackTrace ();
74
+
39
75
failed = true ;
40
- }
41
- }
42
76
43
- /* new Thread() {
44
- @Override
45
- public void run() {
46
- try {
47
- communicator.await();
48
- } catch (InterruptedException e) {
49
- e.printStackTrace();
50
- }
77
+ runOnConnectionFailed ("Failed to connect to OBS" );
78
+ } else {
79
+ throw e ;
51
80
}
52
- }.start(); */
53
- //communicator.;
54
-
81
+ }
55
82
} catch (Throwable t ) {
83
+ System .err .println ("Failed to setup connection with OBS." );
56
84
t .printStackTrace ();
57
- } finally {
85
+ runOnConnectionFailed ("Failed to setup connection with OBS" );
86
+ }
87
+ }
88
+
89
+ public void disconnect () {
90
+ // wait for closed socket connection
91
+ try {
92
+ if (debug ) {
93
+ System .out .println ("Closing connection." );
94
+ }
95
+ communicator .awaitClose (1 , TimeUnit .SECONDS );
96
+ } catch (InterruptedException e ) {
97
+ e .printStackTrace ();
98
+ runOnError ("Error during closing websocket connection" , e );
99
+ }
100
+
101
+ if (!client .isStopped () && !client .isStopping ()) {
58
102
try {
59
- //client.stop();
103
+ if (debug ) {
104
+ System .out .println ("Stopping client." );
105
+ }
106
+ client .stop ();
60
107
} catch (Exception e ) {
61
108
e .printStackTrace ();
109
+ runOnError ("Error during stopping websocket client" , e );
62
110
}
63
111
}
64
112
}
65
113
66
- public OBSRemoteController (String address , boolean debug ) {
67
- this (address , debug , null );
68
- }
69
-
70
114
public boolean isFailed () {
71
115
return failed ;
72
116
}
73
117
74
118
public void getScenes (Callback callback ) {
75
119
communicator .getScenes (callback );
76
- };
120
+ }
121
+
122
+ public void registerOnError (ErrorCallback onError ) {
123
+ this .onError = onError ;
124
+ communicator .registerOnError (onError );
125
+ }
77
126
78
127
public void registerConnectCallback (Callback onConnect ) {
79
128
communicator .registerOnConnect (onConnect );
@@ -84,6 +133,7 @@ public void registerDisconnectCallback(Callback onDisconnect) {
84
133
}
85
134
86
135
public void registerConnectionFailedCallback (StringCallback onConnectionFailed ) {
136
+ this .onConnectionFailed = onConnectionFailed ;
87
137
communicator .registerOnConnectionFailed (onConnectionFailed );
88
138
}
89
139
@@ -132,14 +182,12 @@ public void setCurrentTransition(String transition, Callback callback) {
132
182
}
133
183
134
184
public void changeSceneWithTransition (final String scene , String transition , final Callback callback ) {
135
- communicator .setCurrentTransition (transition , new Callback () {
136
- @ Override
137
- public void run (ResponseBase response ) {
138
- if (!response .getStatus ().equals ("ok" )) {
139
- System .out .println ("Failed to change transition. Pls fix." );
140
- }
141
- communicator .setCurrentScene (scene , callback );
185
+ communicator .setCurrentTransition (transition , response -> {
186
+ if (!response .getStatus ().equals ("ok" )) {
187
+ System .out .println ("Failed to change transition. Pls fix." );
188
+ runOnError ("Error response for changeSceneWithTransition" , new OBSResponseError (response .getError ()));
142
189
}
190
+ communicator .setCurrentScene (scene , callback );
143
191
});
144
192
}
145
193
@@ -244,4 +292,27 @@ public void saveReplayBuffer(Callback callback) {
244
292
communicator .saveReplayBuffer (callback );
245
293
}
246
294
295
+ private void runOnError (String message , Throwable throwable ) {
296
+ if (onError == null ) {
297
+ return ;
298
+ }
299
+
300
+ try {
301
+ onError .run (message , throwable );
302
+ } catch (Exception e ) {
303
+ e .printStackTrace ();
304
+ }
305
+ }
306
+
307
+ private void runOnConnectionFailed (String message ) {
308
+ if (onConnectionFailed == null ) {
309
+ return ;
310
+ }
311
+
312
+ try {
313
+ onConnectionFailed .run (message );
314
+ } catch (Exception e ) {
315
+ e .printStackTrace ();
316
+ }
317
+ }
247
318
}
0 commit comments