玖星尚缘 v1.2.7版本的 MD5 值为:a5d3a854f0a33133433102e91a1193e3
以下内容为反编译后的 DefaultWebSocketAdapter.java 源代码,内容仅作参考
package io.dcloud.feature.weex.adapter;
import android.os.Build;
import android.text.TextUtils;
import android.util.Base64;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
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 io.dcloud.common.util.ThrottleUtil;
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 ThrottleUtil throttleUtil = null;
private WebSocket ws;
@Override
public void connect(String url, final String protocol, IWebSocketAdapter.EventListener listener) {
SSLSocketFactory sSLSocketFactory;
this.eventListener = listener;
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 e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e2) {
e2.printStackTrace();
}
builder.readTimeout(24L, TimeUnit.HOURS);
builder.writeTimeout(24L, TimeUnit.HOURS);
builder.hostnameVerifier(DCloudTrustManager.getHostnameVerifier(false));
Request.Builder builder2 = new Request.Builder();
if (!TextUtils.isEmpty(protocol)) {
builder2.addHeader(IWebSocketAdapter.HEADER_SEC_WEBSOCKET_PROTOCOL, protocol);
}
builder2.addHeader("Origin", "http://localhost");
builder2.url(url);
builder.build().newWebSocket(builder2.build(), new WebSocketListener() {
@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 str : headers.names()) {
hashMap.put(str, headers.values(str).toString());
}
}
@Override
public void onMessage(WebSocket webSocket, String text) {
super.onMessage(webSocket, text);
DefaultWebSocketAdapter.this.eventListener.onMessage(text);
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
super.onMessage(webSocket, bytes);
String encodeToString = Base64.encodeToString(bytes.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 onClosing(WebSocket webSocket, final int code, final String reason) {
super.onClosing(webSocket, code, reason);
if (DefaultWebSocketAdapter.this.throttleUtil == null) {
DefaultWebSocketAdapter.this.throttleUtil = new ThrottleUtil();
}
DefaultWebSocketAdapter.this.throttleUtil.throttlePost(new Runnable() {
@Override
public void run() {
DefaultWebSocketAdapter.this.eventListener.onClose(code, reason, true);
}
}, 10L);
}
@Override
public void onClosed(WebSocket webSocket, final int code, final String reason) {
super.onClosed(webSocket, code, reason);
if (DefaultWebSocketAdapter.this.throttleUtil == null) {
DefaultWebSocketAdapter.this.throttleUtil = new ThrottleUtil();
}
DefaultWebSocketAdapter.this.throttleUtil.throttlePost(new Runnable() {
@Override
public void run() {
DefaultWebSocketAdapter.this.eventListener.onClose(code, reason, true);
}
}, 10L);
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
super.onFailure(webSocket, t, response);
if (t instanceof EOFException) {
DefaultWebSocketAdapter.this.eventListener.onClose(WebSocketCloseCodes.CLOSE_NORMAL.getCode(), WebSocketCloseCodes.CLOSE_NORMAL.name(), true);
} else {
DefaultWebSocketAdapter.this.eventListener.onError(t.getMessage());
}
}
});
}
@Override
public void connect(String url, String protocol, String headerJson, IWebSocketAdapter.EventListener listener) {
Map map;
SSLSocketFactory sSLSocketFactory;
this.eventListener = listener;
OkHttpClient.Builder builder = new OkHttpClient.Builder();
boolean z = false;
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 e) {
e.printStackTrace();
} catch (NoSuchAlgorithmException e2) {
e2.printStackTrace();
}
builder.readTimeout(24L, TimeUnit.HOURS);
builder.writeTimeout(24L, TimeUnit.HOURS);
if (mConnectPool == null) {
mConnectPool = new ConnectionPool();
}
builder.connectionPool(mConnectPool);
builder.hostnameVerifier(DCloudTrustManager.getHostnameVerifier(false));
Request.Builder builder2 = new Request.Builder();
if (!TextUtils.isEmpty(protocol)) {
builder2.addHeader(IWebSocketAdapter.HEADER_SEC_WEBSOCKET_PROTOCOL, protocol);
}
try {
if (!TextUtils.isEmpty(headerJson) && (map = (Map) JSON.parse(headerJson)) != null) {
for (Map.Entry entry : map.entrySet()) {
String str = (String) entry.getKey();
String str2 = (String) entry.getValue();
if ("Origin".equals(str)) {
z = true;
}
if (!TextUtils.isEmpty(str)) {
builder2.addHeader(str, str2);
}
}
}
} catch (Exception e3) {
e3.printStackTrace();
}
if (!z) {
builder2.addHeader("Origin", "http://localhost");
}
builder2.url(url);
builder.build().newWebSocket(builder2.build(), new WebSocketListener() {
@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, String text) {
super.onMessage(webSocket, text);
DefaultWebSocketAdapter.this.eventListener.onMessage(text);
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
super.onMessage(webSocket, bytes);
String encodeToString = Base64.encodeToString(bytes.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 onClosing(WebSocket webSocket, final int code, final String reason) {
super.onClosing(webSocket, code, reason);
if (DefaultWebSocketAdapter.this.throttleUtil == null) {
DefaultWebSocketAdapter.this.throttleUtil = new ThrottleUtil();
}
DefaultWebSocketAdapter.this.throttleUtil.throttlePost(new Runnable() {
@Override
public void run() {
DefaultWebSocketAdapter.this.eventListener.onClose(code, reason, true);
}
}, 10L);
}
@Override
public void onClosed(WebSocket webSocket, final int code, final String reason) {
super.onClosed(webSocket, code, reason);
if (DefaultWebSocketAdapter.this.throttleUtil == null) {
DefaultWebSocketAdapter.this.throttleUtil = new ThrottleUtil();
}
DefaultWebSocketAdapter.this.throttleUtil.throttlePost(new Runnable() {
@Override
public void run() {
DefaultWebSocketAdapter.this.eventListener.onClose(code, reason, true);
}
}, 10L);
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, Response response) {
super.onFailure(webSocket, t, response);
if (t instanceof EOFException) {
DefaultWebSocketAdapter.this.eventListener.onClose(WebSocketCloseCodes.CLOSE_NORMAL.getCode(), WebSocketCloseCodes.CLOSE_NORMAL.name(), true);
} else {
DefaultWebSocketAdapter.this.eventListener.onError(t.getMessage());
}
}
});
}
@Override
public void send(String data) {
try {
JSONObject parseObject = JSON.parseObject(data);
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(data);
return;
} catch (Exception e) {
e.printStackTrace();
reportError(e.getMessage());
return;
}
}
reportError("WebSocket is not ready");
}
public void send(JSONObject data) {
if (this.ws != null) {
try {
String string = data.getString("@type");
if (string != null && string.equals("binary") && data.containsKey("base64")) {
byte[] decode = Base64.decode(data.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 e) {
reportError(e.getMessage());
return;
}
}
reportError("WebSocket is not ready");
}
@Override
public void close(int code, String reason) {
WebSocket webSocket = this.ws;
if (webSocket != null) {
try {
webSocket.close(code, reason);
} catch (Exception e) {
e.printStackTrace();
reportError(e.getMessage());
}
}
}
@Override
public void destroy() {
WebSocket webSocket = this.ws;
if (webSocket != null) {
try {
webSocket.close(WebSocketCloseCodes.CLOSE_GOING_AWAY.getCode(), WebSocketCloseCodes.CLOSE_GOING_AWAY.name());
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void reportError(String message) {
IWebSocketAdapter.EventListener eventListener = this.eventListener;
if (eventListener != null) {
eventListener.onError(message);
}
}
}