Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch web socket client implementation to okhttp library #941

Merged
merged 1 commit into from
Jun 13, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
Expand Down
117 changes: 56 additions & 61 deletions src/main/java/io/appium/java_client/ws/StringWebSocketClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>, CanHandleErrors, CanHandleConnects, CanHandleDisconnects {
private final List<Consumer<String>> messageHandlers = new CopyOnWriteArrayList<>();
private final List<Consumer<Throwable>> errorHandlers = new CopyOnWriteArrayList<>();
private final List<Runnable> connectHandlers = new CopyOnWriteArrayList<>();
private final List<Runnable> 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
Expand Down
53 changes: 0 additions & 53 deletions src/main/java/io/appium/java_client/ws/WebSocketClient.java

This file was deleted.