工作空间 v3.7.4.6版本的 MD5 值为:94c1c0052e9f2e949ae1349e6caf437b
以下内容为反编译后的 WebSocketIOLink.java 源代码,内容仅作参考
package com.zhizhangyi.platform.zpush.internal.longlink.websocket;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import com.google.common.net.HttpHeaders;
import com.zhizhangyi.platform.common.rtc.ZAlarm;
import com.zhizhangyi.platform.zpush.LongLinkInterface;
import com.zhizhangyi.platform.zpush.internal.IPushAckItem;
import com.zhizhangyi.platform.zpush.internal.longlink.ILongLinkResponse;
import com.zhizhangyi.platform.zpush.internal.longlink.socketio.engine.engineio.client.Socket;
import com.zhizhangyi.platform.zpush.internal.utils.ConnectStatus;
import com.zhizhangyi.platform.zpush.internal.utils.Env;
import com.zhizhangyi.platform.zpush.internal.utils.ErrCmdType;
import java.util.Collection;
import java.util.concurrent.TimeUnit;
import okhttp3.HttpUrl;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.WebSocket;
import okhttp3.WebSocketListener;
import okio.ByteString;
import org.json.JSONException;
import org.json.JSONObject;
public class WebSocketIOLink implements LongLinkInterface {
private ZAlarm alarm;
private boolean awaitingPong;
private LongLinkInterface.ILongLinkCallback2 callback;
private final WebSocket.Factory factory;
private final Handler handler;
private LongLinkInterface.IHeartBeatCallback heartBeatCallback;
private boolean isPingTimeout;
private int receivedPongCount;
private ILongLinkResponse responseCallback;
private int sentPingCount;
private final HttpUrl url;
private WebSocket webSocket;
private ConnectStatus status = ConnectStatus.DISCONNECT;
private int pingIntervalInSeconds = (int) TimeUnit.MINUTES.toSeconds(4);
private final class PingRunnable implements Runnable {
PingRunnable() {
}
@Override
public void run() {
if (WebSocketIOLink.this.status == ConnectStatus.DISCONNECT) {
return;
}
int i = WebSocketIOLink.this.awaitingPong ? WebSocketIOLink.this.sentPingCount : -1;
WebSocketIOLink.access$1008(WebSocketIOLink.this);
WebSocketIOLink.this.awaitingPong = true;
if (i != -1) {
WebSocketIOLink.this.isPingTimeout = true;
WebSocketIOLink.this.webSocket.cancel();
return;
}
WebSocketIOLink.this.webSocket.send("PING");
if (WebSocketIOLink.this.heartBeatCallback != null) {
WebSocketIOLink.this.heartBeatCallback.onPing();
}
WebSocketIOLink.this.alarm = new ZAlarm(Env.getContext(), new PingRunnable(), Looper.myLooper(), false, Env.isEnableWakeLock(), false, "PUSH_ALARM_ACTION");
WebSocketIOLink.this.alarm.Start(WebSocketIOLink.this.pingIntervalInSeconds * 1000);
}
}
public WebSocketIOLink(Looper looper, WebSocket.Factory factory, String str, int i, String str2, boolean z) {
this.handler = new Handler(looper);
this.factory = factory;
this.url = new HttpUrl.Builder().scheme(z ? "https" : "http").host(str).port(i).addPathSegments(str2.startsWith("/") ? str2.substring(1) : str2).build();
}
static int access$1008(WebSocketIOLink webSocketIOLink) {
int i = webSocketIOLink.sentPingCount;
webSocketIOLink.sentPingCount = i + 1;
return i;
}
public void cleanUp() {
this.handler.removeCallbacksAndMessages(null);
ZAlarm zAlarm = this.alarm;
if (zAlarm != null) {
zAlarm.Cancel();
}
this.status = ConnectStatus.DISCONNECT;
}
public void onReadPong() {
LongLinkInterface.IHeartBeatCallback iHeartBeatCallback;
Log.i("ZPush", "onReadPong ");
this.receivedPongCount++;
this.awaitingPong = false;
if (this.status == ConnectStatus.DISCONNECT || (iHeartBeatCallback = this.heartBeatCallback) == null) {
return;
}
iHeartBeatCallback.onPong();
}
@Override
public void checkSvrConnect() {
}
@Override
public void connect() {
this.status = ConnectStatus.CONNECTING;
Request.Builder url = new Request.Builder().url(this.url);
url.addHeader(HttpHeaders.ORIGIN, "file://zpush");
this.webSocket = this.factory.newWebSocket(url.build(), new WebSocketListener() {
@Override
public void onClosed(WebSocket webSocket, int i, final String str) {
super.onClosed(webSocket, i, str);
WebSocketIOLink.this.handler.post(new Runnable() {
@Override
public void run() {
if (WebSocketIOLink.this.status != ConnectStatus.DISCONNECT) {
WebSocketIOLink.this.callback.onDisconnect("transport close");
WebSocketIOLink.this.cleanUp();
WebSocketIOLink.this.responseCallback.onResponse(ErrCmdType.kEctServerClose, str);
}
}
});
}
@Override
public void onClosing(WebSocket webSocket, int i, String str) {
webSocket.close(1000, "");
}
@Override
public void onFailure(WebSocket webSocket, final Throwable th, Response response) {
super.onFailure(webSocket, th, response);
final String message = th == null ? "null" : th.getMessage();
WebSocketIOLink.this.handler.post(new Runnable() {
@Override
public void run() {
if (WebSocketIOLink.this.status != ConnectStatus.DISCONNECT) {
ConnectStatus connectStatus = WebSocketIOLink.this.status;
WebSocketIOLink.this.cleanUp();
if (connectStatus == ConnectStatus.CONNECTING) {
WebSocketIOLink.this.callback.onConnectError("connect error", th);
WebSocketIOLink.this.responseCallback.onResponse(ErrCmdType.kEctSocketError, message);
return;
}
LongLinkInterface.ILongLinkCallback2 iLongLinkCallback2 = WebSocketIOLink.this.callback;
Object[] objArr = new Object[2];
objArr[0] = WebSocketIOLink.this.isPingTimeout ? "ping timeout" : "transport error";
objArr[1] = th;
iLongLinkCallback2.onDisconnect(objArr);
WebSocketIOLink.this.responseCallback.onResponse(ErrCmdType.kEctServerClose, message);
}
}
});
}
@Override
public void onMessage(WebSocket webSocket, final String str) {
WebSocketIOLink.this.handler.post(new Runnable() {
@Override
public void run() {
if (WebSocketIOLink.this.status != ConnectStatus.DISCONNECT) {
if ("PONG".equals(str)) {
WebSocketIOLink.this.onReadPong();
return;
}
try {
JSONObject jSONObject = new JSONObject(str);
int i = jSONObject.getInt("type");
JSONObject jSONObject2 = jSONObject.getJSONObject("data");
if (i == 1) {
if (jSONObject2.has(Socket.EVENT_HEARTBEAT)) {
long parseInt = Integer.parseInt(jSONObject2.getString(Socket.EVENT_HEARTBEAT));
if (parseInt < TimeUnit.MINUTES.toSeconds(1L)) {
parseInt = TimeUnit.MINUTES.toSeconds(1L);
}
WebSocketIOLink.this.pingIntervalInSeconds = (int) parseInt;
}
} else if (i == 2) {
IPushAckItem parse = WebSocketIOLink.this.callback.parse(jSONObject2.toString(), new Object[0]);
if (parse == null) {
WebSocketIOLink.this.responseCallback.onResponse(ErrCmdType.kEctHttpFail, "");
} else {
WebSocketIOLink.this.callback.onPush(parse);
}
} else {
throw new JSONException("illegal type: " + i);
}
} catch (Throwable th) {
th.printStackTrace();
}
}
}
});
}
@Override
public void onOpen(WebSocket webSocket, Response response) {
WebSocketIOLink.this.handler.post(new Runnable() {
@Override
public void run() {
if (WebSocketIOLink.this.status != ConnectStatus.DISCONNECT) {
WebSocketIOLink.this.status = ConnectStatus.CONNECTED;
WebSocketIOLink.this.callback.onConnect(Integer.valueOf(WebSocketIOLink.this.pingIntervalInSeconds * 1000), Integer.valueOf(WebSocketIOLink.this.pingIntervalInSeconds * 1000));
WebSocketIOLink.this.alarm = new ZAlarm(Env.getContext(), new PingRunnable(), Looper.myLooper(), false, Env.isEnableWakeLock(), false, "PUSH_ALARM_ACTION");
WebSocketIOLink.this.alarm.Start((int) TimeUnit.MINUTES.toMillis(1L));
}
}
});
}
@Override
public void onMessage(WebSocket webSocket, ByteString byteString) {
super.onMessage(webSocket, byteString);
}
});
}
@Override
public void disconnect(String str) {
if (this.status != ConnectStatus.DISCONNECT) {
this.webSocket.close(1000, "");
if (this.status.atLeast(ConnectStatus.CONNECTED)) {
this.callback.onDisconnect(str);
}
cleanUp();
}
}
@Override
public boolean send(String str) {
WebSocket webSocket = this.webSocket;
if (webSocket == null) {
return false;
}
return webSocket.send(str);
}
@Override
public void setHeartbeatCallback(LongLinkInterface.IHeartBeatCallback iHeartBeatCallback) {
this.heartBeatCallback = iHeartBeatCallback;
}
@Override
public void setLongLinkCallback(LongLinkInterface.ILongLinkCallback2 iLongLinkCallback2) {
this.callback = iLongLinkCallback2;
}
@Override
public void setLongLinkResponse(ILongLinkResponse iLongLinkResponse) {
this.responseCallback = iLongLinkResponse;
}
@Override
public void setProtocolEvent(Collection<String> collection) {
}
@Override
public boolean send(byte[] bArr) {
WebSocket webSocket = this.webSocket;
if (webSocket == null) {
return false;
}
return webSocket.send(ByteString.of(bArr));
}
}