乐健体育 v3.9.1版本的 MD5 值为:87ae8a64955cdd0e43f3559ee8984d75
以下内容为反编译后的 DefaultWebSocketAdapter.java 源代码,内容仅作参考
package io.dcloud.feature.weex.adapter;
import android.os.Build;
import android.text.TextUtils;
import android.util.Base64;
import androidx.annotation.Nullable;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.google.common.net.HttpHeaders;
import com.taobao.weex.appfram.websocket.IWebSocketAdapter;
import com.taobao.weex.appfram.websocket.WebSocketCloseCodes;
import dc.squareup.okhttp3.ConnectionPool;
import dc.squareup.okhttp3.ConnectionSpec;
import dc.squareup.okhttp3.Headers;
import dc.squareup.okhttp3.OkHttpClient;
import dc.squareup.okhttp3.Request;
import dc.squareup.okhttp3.Response;
import dc.squareup.okhttp3.WebSocket;
import dc.squareup.okhttp3.WebSocketListener;
import dc.squareup.okio.ByteString;
import io.dcloud.common.adapter.util.DCloudTrustManager;
import java.io.EOFException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.net.ssl.SSLSocketFactory;
public class DefaultWebSocketAdapter implements IWebSocketAdapter {
private static ConnectionPool mConnectPool;
private IWebSocketAdapter.EventListener eventListener;
private WebSocket ws;
private void reportError(String str) {
IWebSocketAdapter.EventListener eventListener = this.eventListener;
if (eventListener != null) {
eventListener.onError(str);
}
}
@Override
public void close(int i2, String str) {
WebSocket webSocket = this.ws;
if (webSocket != null) {
try {
webSocket.close(i2, str);
} catch (Exception e2) {
e2.printStackTrace();
reportError(e2.getMessage());
}
}
}
@Override
public void connect(String str, @Nullable String str2, IWebSocketAdapter.EventListener eventListener) {
SSLSocketFactory sSLSocketFactory;
this.eventListener = eventListener;
OkHttpClient.Builder builder = new OkHttpClient.Builder();
try {
if (Build.VERSION.SDK_INT < 21) {
sSLSocketFactory = DCloudTrustManager.getSSLSocketFactory("TLSv1.2");
if (sSLSocketFactory != null) {
builder.connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS, ConnectionSpec.CLEARTEXT));
}
} else {
sSLSocketFactory = DCloudTrustManager.getSSLSocketFactory();
}
if (sSLSocketFactory != null) {
builder.sslSocketFactory(sSLSocketFactory);
}
} catch (KeyManagementException e2) {
e2.printStackTrace();
} catch (NoSuchAlgorithmException e3) {
e3.printStackTrace();
}
TimeUnit timeUnit = TimeUnit.HOURS;
builder.readTimeout(24L, timeUnit);
builder.writeTimeout(24L, timeUnit);
builder.hostnameVerifier(DCloudTrustManager.getHostnameVerifier(false));
Request.Builder builder2 = new Request.Builder();
if (!TextUtils.isEmpty(str2)) {
builder2.addHeader("Sec-WebSocket-Protocol", str2);
}
builder2.addHeader(HttpHeaders.ORIGIN, "http://localhost");
builder2.url(str);
builder.build().newWebSocket(builder2.build(), new WebSocketListener() {
@Override
public void onClosed(WebSocket webSocket, int i2, String str3) {
super.onClosed(webSocket, i2, str3);
DefaultWebSocketAdapter.this.eventListener.onClose(i2, str3, true);
}
@Override
public void onClosing(WebSocket webSocket, int i2, String str3) {
super.onClosing(webSocket, i2, str3);
DefaultWebSocketAdapter.this.eventListener.onClose(i2, str3, true);
}
@Override
public void onFailure(WebSocket webSocket, Throwable th, @Nullable Response response) {
super.onFailure(webSocket, th, response);
th.printStackTrace();
if (th instanceof EOFException) {
IWebSocketAdapter.EventListener eventListener2 = DefaultWebSocketAdapter.this.eventListener;
WebSocketCloseCodes webSocketCloseCodes = WebSocketCloseCodes.CLOSE_NORMAL;
eventListener2.onClose(webSocketCloseCodes.getCode(), webSocketCloseCodes.name(), true);
return;
}
DefaultWebSocketAdapter.this.eventListener.onError(th.getMessage());
}
@Override
public void onMessage(WebSocket webSocket, String str3) {
super.onMessage(webSocket, str3);
DefaultWebSocketAdapter.this.eventListener.onMessage(str3);
}
@Override
public void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response);
DefaultWebSocketAdapter.this.ws = webSocket;
DefaultWebSocketAdapter.this.eventListener.onOpen();
Headers headers = response.headers();
HashMap hashMap = new HashMap();
for (String str3 : headers.names()) {
hashMap.put(str3, headers.values(str3).toString());
}
}
@Override
public void onMessage(WebSocket webSocket, ByteString byteString) {
super.onMessage(webSocket, byteString);
String encodeToString = Base64.encodeToString(byteString.toByteArray(), 2);
JSONObject jSONObject = new JSONObject();
jSONObject.put("@type", (Object) "binary");
jSONObject.put("base64", (Object) encodeToString);
DefaultWebSocketAdapter.this.eventListener.onMessage(jSONObject.toJSONString());
}
});
}
@Override
public void destroy() {
WebSocket webSocket = this.ws;
if (webSocket != null) {
try {
WebSocketCloseCodes webSocketCloseCodes = WebSocketCloseCodes.CLOSE_GOING_AWAY;
webSocket.close(webSocketCloseCodes.getCode(), webSocketCloseCodes.name());
} catch (Exception e2) {
e2.printStackTrace();
}
}
}
@Override
public void send(String str) {
try {
JSONObject parseObject = JSON.parseObject(str);
String string = parseObject.getString("@type");
if (string != null && string.equals("binary") && parseObject.containsKey("base64")) {
send(parseObject);
return;
}
} catch (Exception unused) {
}
WebSocket webSocket = this.ws;
if (webSocket != null) {
try {
webSocket.send(str);
return;
} catch (Exception e2) {
e2.printStackTrace();
reportError(e2.getMessage());
return;
}
}
reportError("WebSocket is not ready");
}
public void send(JSONObject jSONObject) {
if (this.ws != null) {
try {
String string = jSONObject.getString("@type");
if (string != null && string.equals("binary") && jSONObject.containsKey("base64")) {
byte[] decode = Base64.decode(jSONObject.getString("base64"), 0);
if (decode != null) {
this.ws.send(ByteString.of(decode));
} else {
reportError("some error occur");
}
} else {
reportError("some error occur");
}
return;
} catch (Exception e2) {
reportError(e2.getMessage());
return;
}
}
reportError("WebSocket is not ready");
}
@Override
public void connect(String str, @Nullable String str2, String str3, IWebSocketAdapter.EventListener eventListener) {
Map map;
SSLSocketFactory sSLSocketFactory;
this.eventListener = eventListener;
OkHttpClient.Builder builder = new OkHttpClient.Builder();
try {
if (Build.VERSION.SDK_INT < 21) {
sSLSocketFactory = DCloudTrustManager.getSSLSocketFactory("TLSv1.2");
if (sSLSocketFactory != null) {
builder.connectionSpecs(Arrays.asList(ConnectionSpec.MODERN_TLS, ConnectionSpec.COMPATIBLE_TLS, ConnectionSpec.CLEARTEXT));
}
} else {
sSLSocketFactory = DCloudTrustManager.getSSLSocketFactory();
}
if (sSLSocketFactory != null) {
builder.sslSocketFactory(sSLSocketFactory);
}
} catch (KeyManagementException e2) {
e2.printStackTrace();
} catch (NoSuchAlgorithmException e3) {
e3.printStackTrace();
}
TimeUnit timeUnit = TimeUnit.HOURS;
builder.readTimeout(24L, timeUnit);
builder.writeTimeout(24L, timeUnit);
if (mConnectPool == null) {
mConnectPool = new ConnectionPool();
}
builder.connectionPool(mConnectPool);
builder.hostnameVerifier(DCloudTrustManager.getHostnameVerifier(false));
Request.Builder builder2 = new Request.Builder();
if (!TextUtils.isEmpty(str2)) {
builder2.addHeader("Sec-WebSocket-Protocol", str2);
}
builder2.addHeader(HttpHeaders.ORIGIN, "http://localhost");
try {
if (!TextUtils.isEmpty(str3) && (map = (Map) JSON.parse(str3)) != null) {
for (Map.Entry entry : map.entrySet()) {
String str4 = (String) entry.getKey();
String str5 = (String) entry.getValue();
if (!TextUtils.isEmpty(str4)) {
builder2.addHeader(str4, str5);
}
}
}
} catch (Exception e4) {
e4.printStackTrace();
}
builder2.url(str);
builder.build().newWebSocket(builder2.build(), new WebSocketListener() {
@Override
public void onClosed(WebSocket webSocket, int i2, String str6) {
super.onClosed(webSocket, i2, str6);
DefaultWebSocketAdapter.this.eventListener.onClose(i2, str6, true);
}
@Override
public void onClosing(WebSocket webSocket, int i2, String str6) {
}
@Override
public void onFailure(WebSocket webSocket, Throwable th, @Nullable Response response) {
super.onFailure(webSocket, th, response);
th.printStackTrace();
if (th instanceof EOFException) {
IWebSocketAdapter.EventListener eventListener2 = DefaultWebSocketAdapter.this.eventListener;
WebSocketCloseCodes webSocketCloseCodes = WebSocketCloseCodes.CLOSE_NORMAL;
eventListener2.onClose(webSocketCloseCodes.getCode(), webSocketCloseCodes.name(), true);
return;
}
DefaultWebSocketAdapter.this.eventListener.onError(th.getMessage());
}
@Override
public void onMessage(WebSocket webSocket, String str6) {
super.onMessage(webSocket, str6);
DefaultWebSocketAdapter.this.eventListener.onMessage(str6);
}
@Override
public void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response);
DefaultWebSocketAdapter.this.ws = webSocket;
DefaultWebSocketAdapter.this.eventListener.onOpen();
Headers headers = response.headers();
HashMap hashMap = new HashMap();
for (String str6 : headers.names()) {
hashMap.put(str6, headers.values(str6).toString());
}
}
@Override
public void onMessage(WebSocket webSocket, ByteString byteString) {
super.onMessage(webSocket, byteString);
String encodeToString = Base64.encodeToString(byteString.toByteArray(), 2);
JSONObject jSONObject = new JSONObject();
jSONObject.put("@type", (Object) "binary");
jSONObject.put("base64", (Object) encodeToString);
DefaultWebSocketAdapter.this.eventListener.onMessage(jSONObject.toJSONString());
}
});
}
}