Skip to content

Commit 95f0964

Browse files
author
Mykola Mokhnach
authored
Init web socket clients lazily (#912)
1 parent 68dccaa commit 95f0964

File tree

4 files changed

+37
-14
lines changed

4 files changed

+37
-14
lines changed

src/main/java/io/appium/java_client/android/AndroidDriver.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import io.appium.java_client.screenrecording.CanRecordScreen;
3636
import io.appium.java_client.service.local.AppiumDriverLocalService;
3737
import io.appium.java_client.service.local.AppiumServiceBuilder;
38+
import io.appium.java_client.ws.StringWebSocketClient;
3839
import org.openqa.selenium.Capabilities;
3940
import org.openqa.selenium.WebElement;
4041
import org.openqa.selenium.remote.HttpCommandExecutor;
@@ -66,6 +67,8 @@ public class AndroidDriver<T extends WebElement>
6667

6768
private static final String ANDROID_PLATFORM = MobilePlatform.ANDROID;
6869

70+
private StringWebSocketClient logcatClient;
71+
6972
/**
7073
* Creates a new instance based on command {@code executor} and {@code capabilities}.
7174
*
@@ -193,4 +196,12 @@ public AndroidBatteryInfo getBatteryInfo() {
193196
return new AndroidBatteryInfo((Map<String, Object>) execute(EXECUTE_SCRIPT, ImmutableMap.of(
194197
"script", "mobile: batteryInfo", "args", Collections.emptyList())));
195198
}
199+
200+
@Override
201+
public synchronized StringWebSocketClient getLogcatClient() {
202+
if (logcatClient == null) {
203+
logcatClient = new StringWebSocketClient();
204+
}
205+
return logcatClient;
206+
}
196207
}

src/main/java/io/appium/java_client/android/ListensToLogcatMessages.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import java.util.function.Consumer;
3232

3333
public interface ListensToLogcatMessages extends ExecutesMethod {
34-
StringWebSocketClient logcatClient = new StringWebSocketClient();
34+
StringWebSocketClient getLogcatClient();
3535

3636
/**
3737
* Start logcat messages broadcast via web socket.
@@ -68,7 +68,7 @@ default void startLogcatBroadcast(String host, int port) {
6868
} catch (URISyntaxException e) {
6969
throw new IllegalArgumentException(e);
7070
}
71-
logcatClient.connect(endpointUri);
71+
getLogcatClient().connect(endpointUri);
7272
}
7373

7474
/**
@@ -80,7 +80,7 @@ default void startLogcatBroadcast(String host, int port) {
8080
* @param handler a function, which accepts a single argument, which is the actual log message
8181
*/
8282
default void addLogcatMessagesListener(Consumer<String> handler) {
83-
logcatClient.addMessageHandler(handler);
83+
getLogcatClient().addMessageHandler(handler);
8484
}
8585

8686
/**
@@ -92,7 +92,7 @@ default void addLogcatMessagesListener(Consumer<String> handler) {
9292
* @param handler a function, which accepts a single argument, which is the actual exception instance
9393
*/
9494
default void addLogcatErrorsListener(Consumer<Throwable> handler) {
95-
logcatClient.addErrorHandler(handler);
95+
getLogcatClient().addErrorHandler(handler);
9696
}
9797

9898
/**
@@ -105,7 +105,7 @@ default void addLogcatErrorsListener(Consumer<Throwable> handler) {
105105
* connected to the web socket
106106
*/
107107
default void addLogcatConnectionListener(Runnable handler) {
108-
logcatClient.addConnectionHandler(handler);
108+
getLogcatClient().addConnectionHandler(handler);
109109
}
110110

111111
/**
@@ -118,14 +118,14 @@ default void addLogcatConnectionListener(Runnable handler) {
118118
* disconnected from the web socket
119119
*/
120120
default void addLogcatDisconnectionListener(Runnable handler) {
121-
logcatClient.addDisconnectionHandler(handler);
121+
getLogcatClient().addDisconnectionHandler(handler);
122122
}
123123

124124
/**
125125
* Removes all existing logcat handlers.
126126
*/
127127
default void removeAllLogcatListeners() {
128-
logcatClient.removeAllHandlers();
128+
getLogcatClient().removeAllHandlers();
129129
}
130130

131131
/**

src/main/java/io/appium/java_client/ios/IOSDriver.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import io.appium.java_client.screenrecording.CanRecordScreen;
3535
import io.appium.java_client.service.local.AppiumDriverLocalService;
3636
import io.appium.java_client.service.local.AppiumServiceBuilder;
37+
import io.appium.java_client.ws.StringWebSocketClient;
3738
import org.openqa.selenium.Alert;
3839
import org.openqa.selenium.Capabilities;
3940
import org.openqa.selenium.WebElement;
@@ -68,6 +69,8 @@ public class IOSDriver<T extends WebElement>
6869

6970
private static final String IOS_PLATFORM = MobilePlatform.IOS;
7071

72+
private StringWebSocketClient syslogClient;
73+
7174
/**
7275
* Creates a new instance based on command {@code executor} and {@code capabilities}.
7376
*
@@ -225,4 +228,12 @@ class IOSAlert implements Alert {
225228
}
226229

227230
}
231+
232+
@Override
233+
public synchronized StringWebSocketClient getSyslogClient() {
234+
if (syslogClient == null) {
235+
syslogClient = new StringWebSocketClient();
236+
}
237+
return syslogClient;
238+
}
228239
}

src/main/java/io/appium/java_client/ios/ListensToSyslogMessages.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
import java.util.function.Consumer;
3232

3333
public interface ListensToSyslogMessages extends ExecutesMethod {
34-
StringWebSocketClient syslogClient = new StringWebSocketClient();
34+
35+
StringWebSocketClient getSyslogClient();
3536

3637
/**
3738
* Start syslog messages broadcast via web socket.
@@ -68,7 +69,7 @@ default void startSyslogBroadcast(String host, int port) {
6869
} catch (URISyntaxException e) {
6970
throw new IllegalArgumentException(e);
7071
}
71-
syslogClient.connect(endpointUri);
72+
getSyslogClient().connect(endpointUri);
7273
}
7374

7475
/**
@@ -80,7 +81,7 @@ default void startSyslogBroadcast(String host, int port) {
8081
* @param handler a function, which accepts a single argument, which is the actual log message
8182
*/
8283
default void addSyslogMessagesListener(Consumer<String> handler) {
83-
syslogClient.addMessageHandler(handler);
84+
getSyslogClient().addMessageHandler(handler);
8485
}
8586

8687
/**
@@ -92,7 +93,7 @@ default void addSyslogMessagesListener(Consumer<String> handler) {
9293
* @param handler a function, which accepts a single argument, which is the actual exception instance
9394
*/
9495
default void addSyslogErrorsListener(Consumer<Throwable> handler) {
95-
syslogClient.addErrorHandler(handler);
96+
getSyslogClient().addErrorHandler(handler);
9697
}
9798

9899
/**
@@ -105,7 +106,7 @@ default void addSyslogErrorsListener(Consumer<Throwable> handler) {
105106
* connected to the web socket
106107
*/
107108
default void addSyslogConnectionListener(Runnable handler) {
108-
syslogClient.addConnectionHandler(handler);
109+
getSyslogClient().addConnectionHandler(handler);
109110
}
110111

111112
/**
@@ -118,14 +119,14 @@ default void addSyslogConnectionListener(Runnable handler) {
118119
* disconnected from the web socket
119120
*/
120121
default void addSyslogDisconnectionListener(Runnable handler) {
121-
syslogClient.addDisconnectionHandler(handler);
122+
getSyslogClient().addDisconnectionHandler(handler);
122123
}
123124

124125
/**
125126
* Removes all existing syslog handlers.
126127
*/
127128
default void removeAllSyslogListeners() {
128-
syslogClient.removeAllHandlers();
129+
getSyslogClient().removeAllHandlers();
129130
}
130131

131132
/**

0 commit comments

Comments
 (0)