|
@@ -15,6 +15,7 @@ import com.neovisionaries.ws.client.WebSocketAdapter;
|
15
|
15
|
import com.neovisionaries.ws.client.WebSocketException;
|
16
|
16
|
import com.neovisionaries.ws.client.WebSocketFactory;
|
17
|
17
|
import com.neovisionaries.ws.client.WebSocketFrame;
|
|
18
|
+import com.xhly.corelib.utils.LogUtils;
|
18
|
19
|
import com.xhly.corelib.utils.NetworkUtils;
|
19
|
20
|
|
20
|
21
|
import java.io.IOException;
|
|
@@ -99,13 +100,13 @@ public abstract class LongConnService extends Service {
|
99
|
100
|
@Override
|
100
|
101
|
public void onCreate() {
|
101
|
102
|
super.onCreate();
|
102
|
|
- Log.i(TAG, "onCreate()............");
|
|
103
|
+ LogUtils.i(TAG, "onCreate()............");
|
103
|
104
|
mListener = new WsListener();
|
104
|
105
|
}
|
105
|
106
|
|
106
|
107
|
@Override
|
107
|
108
|
public int onStartCommand(Intent intent, int flags, int startId) {
|
108
|
|
- Log.i(TAG, "onStartCommand()............");
|
|
109
|
+ LogUtils.i(TAG, "onStartCommand()............");
|
109
|
110
|
closeWebsocket = false;
|
110
|
111
|
if (mListener == null) {
|
111
|
112
|
mListener = new WsListener();
|
|
@@ -116,7 +117,7 @@ public abstract class LongConnService extends Service {
|
116
|
117
|
|
117
|
118
|
@Override
|
118
|
119
|
public void onDestroy() {
|
119
|
|
- Log.i(TAG, "onDestroy()............");
|
|
120
|
+ LogUtils.i(TAG, "onDestroy()............");
|
120
|
121
|
if (heartSubscription != null) {
|
121
|
122
|
heartSubscription.dispose();
|
122
|
123
|
}
|
|
@@ -194,7 +195,7 @@ public abstract class LongConnService extends Service {
|
194
|
195
|
webSocket = null;
|
195
|
196
|
}
|
196
|
197
|
try {
|
197
|
|
- Log.i(TAG, "第一次连接: " + connectUrl);
|
|
198
|
+ LogUtils.i(TAG, "第一次连接: " + connectUrl);
|
198
|
199
|
// 异步连接
|
199
|
200
|
webSocket = createWebSocket();
|
200
|
201
|
setStatus(WsStatus.CONNECTING);
|
|
@@ -214,7 +215,7 @@ public abstract class LongConnService extends Service {
|
214
|
215
|
}
|
215
|
216
|
|
216
|
217
|
private WebSocket createWebSocket() throws IOException {
|
217
|
|
- Log.i(TAG, "创建连接:createWebSocket()");
|
|
218
|
+ LogUtils.i(TAG, "创建连接:createWebSocket()");
|
218
|
219
|
return new WebSocketFactory().createSocket(connectUrl, CONNECT_TIMEOUT)
|
219
|
220
|
//设置帧队列最大值为5
|
220
|
221
|
.setFrameQueueSize(FRAME_QUEUE_SIZE)
|
|
@@ -232,7 +233,7 @@ public abstract class LongConnService extends Service {
|
232
|
233
|
reconnectCount = 0;
|
233
|
234
|
isReConnect = false;
|
234
|
235
|
closeWebsocket = true;
|
235
|
|
- Log.i(TAG, "重连失败网络不可用,当前closeWebsocket==" + closeWebsocket);
|
|
236
|
+ LogUtils.i(TAG, "重连失败网络不可用,当前closeWebsocket==" + closeWebsocket);
|
236
|
237
|
return;
|
237
|
238
|
}
|
238
|
239
|
if (HAS_HEART_RECEIVE) {
|
|
@@ -314,7 +315,7 @@ public abstract class LongConnService extends Service {
|
314
|
315
|
@Override
|
315
|
316
|
public void onConnected(final WebSocket websocket, Map<String, List<String>> headers) throws Exception {
|
316
|
317
|
super.onConnected(websocket, headers);
|
317
|
|
- Log.i(TAG, "连接成功:" + headers.toString());
|
|
318
|
+ LogUtils.i(TAG, "连接成功:" + headers.toString());
|
318
|
319
|
connected = true;
|
319
|
320
|
setStatus(WsStatus.CONNECT_SUCCESS);
|
320
|
321
|
cancelReconnect();// 连接成功的时候取消重连,初始化连接次数
|
|
@@ -325,13 +326,13 @@ public abstract class LongConnService extends Service {
|
325
|
326
|
Observable.interval(HEART_INTERVAL, TimeUnit.SECONDS).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<Long>() {
|
326
|
327
|
@Override
|
327
|
328
|
public void onSubscribe(Disposable d) {
|
328
|
|
- Log.d(TAG, "订阅开始");
|
|
329
|
+ LogUtils.d(TAG,"订阅开始");
|
329
|
330
|
heartSubscription = d;
|
330
|
331
|
}
|
331
|
332
|
|
332
|
333
|
@Override
|
333
|
334
|
public void onNext(Long aLong) {
|
334
|
|
- Log.v(TAG, "发送心跳");
|
|
335
|
+ LogUtils.d(TAG,"发送心跳");
|
335
|
336
|
startHeartInterval(websocket);
|
336
|
337
|
}
|
337
|
338
|
|
|
@@ -352,7 +353,7 @@ public abstract class LongConnService extends Service {
|
352
|
353
|
public void onConnectError(WebSocket websocket, WebSocketException exception) throws Exception {
|
353
|
354
|
super.onConnectError(websocket, exception);
|
354
|
355
|
exception.printStackTrace();
|
355
|
|
- Log.i(TAG, "连接错误: " + exception.getMessage());
|
|
356
|
+ LogUtils.i(TAG, "连接错误: " + exception.getMessage());
|
356
|
357
|
HAS_HEART_RECEIVE = false;
|
357
|
358
|
setStatus(WsStatus.CONNECT_FAIL);
|
358
|
359
|
connected = false;
|
|
@@ -364,7 +365,7 @@ public abstract class LongConnService extends Service {
|
364
|
365
|
@Override
|
365
|
366
|
public void onDisconnected(WebSocket websocket, WebSocketFrame serverCloseFrame, WebSocketFrame clientCloseFrame, boolean closedByServer) throws Exception {
|
366
|
367
|
super.onDisconnected(websocket, serverCloseFrame, clientCloseFrame, closedByServer);
|
367
|
|
- Log.i(TAG, "断开连接,是否重连" + isReConnect + ",当前closeWebsocket==" + closeWebsocket);
|
|
368
|
+ LogUtils.i(TAG, "断开连接,是否重连" + isReConnect + ",当前closeWebsocket==" + closeWebsocket);
|
368
|
369
|
HAS_HEART_RECEIVE = false;
|
369
|
370
|
connected = false;
|
370
|
371
|
if (heartSubscription != null) {
|