From 19bee17c17f4ecd4c026f28c7abe5536c3c83d4a Mon Sep 17 00:00:00 2001 From: Mykola Mokhnach Date: Wed, 13 Jun 2018 16:34:31 +0200 Subject: [PATCH] Switch web socket client implementation to okhttp library --- build.gradle | 3 - .../android/ListensToLogcatMessages.java | 1 + .../java_client/ws/StringWebSocketClient.java | 117 +++++++++--------- .../java_client/ws/WebSocketClient.java | 53 -------- 4 files changed, 57 insertions(+), 117 deletions(-) delete mode 100644 src/main/java/io/appium/java_client/ws/WebSocketClient.java diff --git a/build.gradle b/build.gradle index 32a5092f8..dd99d9612 100644 --- a/build.gradle +++ b/build.gradle @@ -76,9 +76,6 @@ dependencies { compile 'org.springframework:spring-context:5.0.5.RELEASE' compile 'org.aspectj:aspectjweaver:1.9.1' compile 'org.openpnp:opencv:3.2.0-1' - compile 'javax.websocket:javax.websocket-api:1.1' - compile 'org.glassfish.tyrus:tyrus-client:1.13.1' - compile 'org.glassfish.tyrus:tyrus-container-grizzly:1.2.1' testCompile 'junit:junit:4.12' testCompile 'org.hamcrest:hamcrest-library:1.3' diff --git a/src/main/java/io/appium/java_client/android/ListensToLogcatMessages.java b/src/main/java/io/appium/java_client/android/ListensToLogcatMessages.java index bbc560aac..4fffed837 100644 --- a/src/main/java/io/appium/java_client/android/ListensToLogcatMessages.java +++ b/src/main/java/io/appium/java_client/android/ListensToLogcatMessages.java @@ -132,6 +132,7 @@ default void removeAllLogcatListeners() { * Stops logcat messages broadcast via web socket. */ default void stopLogcatBroadcast() { + removeAllLogcatListeners(); execute(EXECUTE_SCRIPT, ImmutableMap.of("script", "mobile: stopLogsBroadcast", "args", Collections.emptyList())); } diff --git a/src/main/java/io/appium/java_client/ws/StringWebSocketClient.java b/src/main/java/io/appium/java_client/ws/StringWebSocketClient.java index 73fabf13b..d7ebe559e 100644 --- a/src/main/java/io/appium/java_client/ws/StringWebSocketClient.java +++ b/src/main/java/io/appium/java_client/ws/StringWebSocketClient.java @@ -16,92 +16,87 @@ package io.appium.java_client.ws; -import org.openqa.selenium.WebDriverException; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import okhttp3.WebSocket; +import okhttp3.WebSocketListener; -import java.io.IOException; import java.net.URI; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; +import java.util.concurrent.TimeUnit; import java.util.function.Consumer; -import javax.websocket.ClientEndpoint; -import javax.websocket.OnClose; -import javax.websocket.OnError; -import javax.websocket.OnMessage; -import javax.websocket.OnOpen; -import javax.websocket.Session; - -@ClientEndpoint -public class StringWebSocketClient extends WebSocketClient implements + +import javax.annotation.Nullable; + +public class StringWebSocketClient extends WebSocketListener implements CanHandleMessages, CanHandleErrors, CanHandleConnects, CanHandleDisconnects { private final List> messageHandlers = new CopyOnWriteArrayList<>(); private final List> errorHandlers = new CopyOnWriteArrayList<>(); private final List connectHandlers = new CopyOnWriteArrayList<>(); private final List disconnectHandlers = new CopyOnWriteArrayList<>(); - private volatile Session session; + private volatile boolean isListening = false; - @Override - public void connect(URI endpoint) { - if (session != null) { - if (endpoint.equals(this.getEndpoint())) { - return; - } - - removeAllHandlers(); - try { - session.close(); - } catch (IOException e) { - // ignore - } - session = null; - } - super.connect(endpoint); + private URI endpoint; + + private void setEndpoint(URI endpoint) { + this.endpoint = endpoint; + } + + @Nullable + public URI getEndpoint() { + return this.endpoint; + } + + public boolean isListening() { + return isListening; } /** - * This event if fired when the client is successfully - * connected to a web socket. + * Connects web socket client. * - * @param session the actual web socket session instance + * @param endpoint The full address of an endpoint to connect to. + * Usually starts with 'ws://'. */ - @OnOpen - public void onOpen(Session session) { - this.session = session; + public void connect(URI endpoint) { + if (endpoint.equals(this.getEndpoint()) && isListening) { + return; + } + + OkHttpClient client = new OkHttpClient.Builder() + .readTimeout(0, TimeUnit.MILLISECONDS) + .build(); + Request request = new Request.Builder() + .url(endpoint.toString()) + .build(); + client.newWebSocket(request, this); + client.dispatcher().executorService().shutdown(); + + setEndpoint(endpoint); + } + + @Override + public void onOpen(WebSocket webSocket, Response response) { getConnectionHandlers().forEach(Runnable::run); + isListening = true; } - /** - * This event if fired when the client is - * disconnected from a web socket. - */ - @OnClose - public void onClose() { - this.session = null; + @Override + public void onClosing(WebSocket webSocket, int code, String reason) { getDisconnectionHandlers().forEach(Runnable::run); + isListening = false; } - /** - * This event if fired when there is an unexpected - * error in web socket connection. - * - * @param cause the actual error reason - */ - @OnError - public void onError(Throwable cause) { - this.session = null; - getErrorHandlers().forEach(x -> x.accept(cause)); - throw new WebDriverException(cause); + @Override + public void onFailure(WebSocket webSocket, Throwable t, Response response) { + getErrorHandlers().forEach(x -> x.accept(t)); } - /** - * This event if fired when there is a - * new message from the web socket. - * - * @param message the actual message content. - */ - @OnMessage - public void onMessage(String message) { - getMessageHandlers().forEach(x -> x.accept(message)); + @Override + public void onMessage(WebSocket webSocket, String text) { + getMessageHandlers().forEach(x -> x.accept(text)); } @Override diff --git a/src/main/java/io/appium/java_client/ws/WebSocketClient.java b/src/main/java/io/appium/java_client/ws/WebSocketClient.java deleted file mode 100644 index 0101e5d2f..000000000 --- a/src/main/java/io/appium/java_client/ws/WebSocketClient.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * See the NOTICE file distributed with this work for additional - * information regarding copyright ownership. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.appium.java_client.ws; - -import org.openqa.selenium.WebDriverException; - -import java.io.IOException; -import java.net.URI; -import javax.websocket.ContainerProvider; -import javax.websocket.DeploymentException; - -public abstract class WebSocketClient { - private URI endpoint; - - private void setEndpoint(URI endpoint) { - this.endpoint = endpoint; - } - - public URI getEndpoint() { - return this.endpoint; - } - - /** - * Connects web socket client. - * - * @param endpoint The full address of an endpoint to connect to. - * Usually starts with 'ws://'. - */ - public void connect(URI endpoint) { - try { - ContainerProvider - .getWebSocketContainer() - .connectToServer(this, endpoint); - setEndpoint(endpoint); - } catch (IOException | DeploymentException e) { - throw new WebDriverException(e); - } - } -}