Commit 88210ec0 by patpat

优化MainWebSocket

parent fec40197
......@@ -5,6 +5,7 @@ import android.util.SparseArray;
import com.bgycc.smartcanteen.AppConfig;
import com.bgycc.smartcanteen.action.ActionEnum;
import com.bgycc.smartcanteen.action.PayOfflineAction;
import com.bgycc.smartcanteen.helper.TimerHelper;
import com.bgycc.smartcanteen.server.websocket.event.ConnectStateEvent;
import com.bgycc.smartcanteen.server.websocket.event.RecvMessageEvent;
import com.bgycc.smartcanteen.server.websocket.event.SendMessageEvent;
......@@ -22,9 +23,6 @@ import java.net.URI;
import java.net.URISyntaxException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class MainWebSocket extends WebSocketClient {
......@@ -48,7 +46,6 @@ public class MainWebSocket extends WebSocketClient {
private static MainWebSocket sInstance;
private static String sDeviceSN;
private static ScheduledExecutorService sKeepAliveExec;
public static void initialize() {
if (sInstance == null) {
......@@ -59,27 +56,18 @@ public class MainWebSocket extends WebSocketClient {
EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.CONNECTING));
sInstance = new MainWebSocket(new URI(String.format(AppConfig.getMainWebSocketServerUrl(), sDeviceSN)));
sInstance.connect();
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
if (sKeepAliveExec == null) {
sKeepAliveExec = Executors.newSingleThreadScheduledExecutor();
sKeepAliveExec.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
if (sInstance == null) return;
TimerHelper.INSTANCE.loop(new Runnable() {
@Override
public void run() {
if (sInstance.isClosed()) {
EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.RECONNECTING));
sInstance.reconnect();
EventBus.getDefault().post(new ConnectStateEvent(ConnectStateEvent.RECONNECTING));
}
} catch (Exception e) {
Log.e(TAG, "sKeepAliveExec error");
e.printStackTrace();
}
}
}, 0, 1000, TimeUnit.MILLISECONDS);
}, 1000);
} catch (URISyntaxException e) {
e.printStackTrace();
}
}
}
......@@ -91,21 +79,6 @@ public class MainWebSocket extends WebSocketClient {
return sInstance.isOpen();
}
public static void payOnline(String payCode, String payCodeType, Response callback) {
if (!isInited()) return;
try {
JSONObject json = new JSONObject();
json.put(FieldEnum.equipmentNo.name(), sDeviceSN);
json.put(FieldEnum.payCode.name(), payCode);
json.put(FieldEnum.terminalType.name(), payCodeType);
json.put(FieldEnum.time.name(), new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'", Locale.getDefault()).format(new Date()));
sInstance.send(ActionEnum.PAY_ONLINE.name(), json, callback);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void action(String action, JSONObject params, Response callback) {
if (!isInited()) return;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment