| | |
| | | |
| | | import android.content.Context; |
| | | import android.os.Handler; |
| | | import android.text.TextUtils; |
| | | import android.util.Log; |
| | | |
| | | import com.dayu.recharge.dao.BaseDaoSingleton; |
| | | import com.dayu.recharge.dbBean.AdminDataBean; |
| | | import com.dayu.recharge.dbBean.DeviceNumber; |
| | | import com.dayu.recharge.dbBean.IpBean; |
| | | import com.easysocket.EasySocket; |
| | | import com.easysocket.entity.OriginReadData; |
| | |
| | | import com.easysocket.interfaces.conn.ISocketActionListener; |
| | | import com.easysocket.interfaces.conn.SocketActionListener; |
| | | import com.dayu.recharge.MyApplication; |
| | | import com.dayu.recharge.utils.ToastUtil; |
| | | import com.dayu.recharge.utils.WSHelper; |
| | | import com.easysocket.utils.LogUtil; |
| | | |
| | | |
| | | /** |
| | |
| | | public class SocketNet { |
| | | public static SocketNet socketNet; |
| | | SocketCallBack myCallBack; |
| | | byte[] dataMessage; |
| | | // byte[] dataMessage; |
| | | boolean isTest = false; |
| | | Context mContext; |
| | | |
| | |
| | | // 发送 |
| | | // EasySocket.getInstance().upMessage(dataMessage); |
| | | if (EasySocket.getInstance().getDefconnection() != null) { |
| | | EasySocket.getInstance().disconnect(false); |
| | | EasySocket.getInstance().connect(); |
| | | this.dataMessage = dataMessage; |
| | | // EasySocket.getInstance().disconnect(false); |
| | | // EasySocket.getInstance().connect(); |
| | | EasySocket.getInstance().upMessage(dataMessage); |
| | | // this.dataMessage = dataMessage; |
| | | } else { |
| | | MyApplication.myApplication.initEasySocket(false, null); |
| | | } |
| | |
| | | isTest = true; |
| | | mContext = context; |
| | | if (EasySocket.getInstance().getDefOptions().getSocketAddress() != null && !MyApplication.myApplication.isDoInitMachine) { |
| | | EasySocket.getInstance().disconnect(false); |
| | | EasySocket.getInstance().connect(); |
| | | // EasySocket.getInstance().disconnect(false); |
| | | // EasySocket.getInstance().connect(); |
| | | } else { |
| | | // ToastUtil.show(mContext, "Socket地址为空"); |
| | | } |
| | |
| | | @Override |
| | | public void onSocketConnSuccess(SocketAddress socketAddress) { |
| | | Log.i("SocketActionListener", "连接成功"); |
| | | String initCode = WSHelper.getInstance(MyApplication.myApplication).get("initCode", ""); |
| | | if (TextUtils.isEmpty(initCode) && !MyApplication.myApplication.isDoInitMachine) { |
| | | MyApplication.myApplication.isConncet = true; |
| | | DeviceNumber initCode = BaseDaoSingleton.getInstance(MyApplication.myApplication).deviceNumberDao().findFirst(); |
| | | if (initCode == null && !MyApplication.myApplication.isDoInitMachine) { |
| | | new Handler().postDelayed(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | |
| | | }, 500); |
| | | |
| | | } |
| | | if (dataMessage != null) { |
| | | EasySocket.getInstance().upMessage(dataMessage); |
| | | } |
| | | // if (dataMessage != null) { |
| | | // EasySocket.getInstance().upMessage(dataMessage); |
| | | // } |
| | | if (isTest) { |
| | | MyApplication.myApplication.isConncet = true; |
| | | EasySocket.getInstance().disconnect(false); |
| | | // EasySocket.getInstance().disconnect(false); |
| | | } else { |
| | | |
| | | } |
| | |
| | | */ |
| | | @Override |
| | | public void onSocketConnFail(SocketAddress socketAddress, boolean isNeedReconnect) { |
| | | // isConnected = false; |
| | | MyApplication.myApplication.isConncet = false; |
| | | Log.i("SocketActionListener", "socket连接失败"); |
| | | if (isTest) { |
| | | ToastUtil.show(mContext, "连接失败,请检查网络"); |
| | | // ToastUtil.show(mContext, "连接失败,请检查网络"); |
| | | } else { |
| | | if (myCallBack != null) { |
| | | myCallBack.onSocketError(-1, "socket连接失败,请检查网络"); |
| | |
| | | SocketData socketData = new SocketData(); |
| | | socketData.setBodyData(originReadData.getBodyBytes()); |
| | | socketData.setHeaderData(originReadData.getHeaderData()); |
| | | dataMessage = null; |
| | | // dataMessage = null; |
| | | if (myCallBack != null) { |
| | | myCallBack.onSocketResponse(socketData); |
| | | } |