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

support okhttp-ws 2.7.5 #54

Open
wants to merge 1 commit into
base: tb-release
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public class OkHttpSocketClient extends SocketClient {
private static final String CLASS_BUFFER = "okio.Buffer";
private static final String CLASS_BUFFER_SOURCE = "okio.BufferedSource";

// 2.7.5
private static final String CLASS_REQUEST_BODY = "com.squareup.okhttp.RequestBody";

static {
String[] classNames = new String[]{
CLASS_WEBSOCKET,
Expand All @@ -46,7 +49,7 @@ public class OkHttpSocketClient extends SocketClient {
CLASS_REQUEST,
CLASS_REQUEST_BUILDER,
CLASS_BUFFER,
CLASS_BUFFER_SOURCE
CLASS_BUFFER_SOURCE, CLASS_REQUEST_BODY
};
for (String className : classNames) {
sClazzMap.put(className, ReflectionUtil.tryGetClassForName(className));
Expand All @@ -64,6 +67,7 @@ public class OkHttpSocketClient extends SocketClient {

private Class mBufferClazz = sClazzMap.get(CLASS_BUFFER);
private Class mBufferedSourceClazz = sClazzMap.get(CLASS_BUFFER_SOURCE);
private Class mRequestBodyClazz = sClazzMap.get(CLASS_REQUEST_BODY);

public OkHttpSocketClient(DebugServerProxy proxy) {
super(proxy);
Expand Down Expand Up @@ -168,12 +172,19 @@ protected void sendProtocolMessage(int requestID, String message) {
Method sendMessageMethod = ReflectionUtil.tryGetMethod(mWebSocketClazz,
"sendMessage", new Class[]{mMediaTypeClazz, mBufferClazz});

Object buffer = mBufferClazz.newInstance();
Method writeUtf8 = ReflectionUtil.tryGetMethod(mBufferClazz, "writeUtf8",
new Class[]{String.class});

ReflectionUtil.tryInvokeMethod(mWebSocket, sendMessageMethod, textValue,
ReflectionUtil.tryInvokeMethod(buffer, writeUtf8, message));
if (sendMessageMethod != null) {
Object buffer = mBufferClazz.newInstance();
Method writeUtf8 = ReflectionUtil.tryGetMethod(mBufferClazz, "writeUtf8",
new Class[]{String.class});

ReflectionUtil.tryInvokeMethod(mWebSocket, sendMessageMethod, textValue,
ReflectionUtil.tryInvokeMethod(buffer, writeUtf8, message));
} else { // okhttp 2.7.5
sendMessageMethod = ReflectionUtil.tryGetMethod(mWebSocketClazz,
"sendMessage", new Class[]{mRequestBodyClazz});
Method create = ReflectionUtil.tryGetMethod(mRequestBodyClazz, "create", textValue.getClass(), String.class);
ReflectionUtil.tryInvokeMethod(mWebSocket, sendMessageMethod, ReflectionUtil.tryInvokeMethod(mRequestBodyClazz, create, textValue, message));
}
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InstantiationException e) {
Expand Down Expand Up @@ -205,7 +216,12 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
} else if ("onFailure".equals(method.getName())) {
abort("Websocket onFailure", (IOException) args[0]);
} else if ("onMessage".equals(method.getName())) {
Object bufferedSource = mBufferedSourceClazz.cast(args[0]);
Object bufferedSource = null;
if (args[0].getClass().equals(mBufferedSourceClazz)) {
bufferedSource = mBufferedSourceClazz.cast(args[0]);
} else { // ok2.7.5
bufferedSource = ReflectionUtil.tryInvokeMethod(args[0], ReflectionUtil.tryGetMethod(args[0].getClass(), "source"));
}
Method readUtf8 = ReflectionUtil.tryGetMethod(mBufferedSourceClazz, "readUtf8");
try {
String message = (String) ReflectionUtil.tryInvokeMethod(bufferedSource, readUtf8);
Expand Down