|
1
|
|
-package com.qnbar.smc.service;
|
|
2
|
|
-
|
|
3
|
|
-import android.app.Service;
|
|
4
|
|
-import android.content.Context;
|
|
5
|
|
-import android.content.Intent;
|
|
6
|
|
-import android.os.IBinder;
|
|
7
|
|
-import android.text.TextUtils;
|
|
8
|
|
-import android.util.Log;
|
|
9
|
|
-import android.widget.Toast;
|
|
10
|
|
-import com.gimi.common.cinema.model.MessageEvent;
|
|
11
|
|
-import com.gimi.common.cinema.model.RoomInfo;
|
|
12
|
|
-import com.gimi.common.cinema.utils.SystemUtils;
|
|
13
|
|
-import com.gimi.common.cinema.utils.Utils;
|
|
14
|
|
-import com.google.gson.Gson;
|
|
15
|
|
-import com.google.gson.JsonSyntaxException;
|
|
16
|
|
-import com.qnbar.smc.utils.LightOperationUtils;
|
|
17
|
|
-import com.xgimi.gimicinema.activity.QrCodeShowActivity;
|
|
18
|
|
-import com.xgimi.gimicinema.application.FangTangApplication;
|
|
19
|
|
-import com.xgimi.smartscreen.encrypt.AuthCode;
|
|
20
|
|
-import org.greenrobot.eventbus.EventBus;
|
|
21
|
|
-
|
|
22
|
|
-import java.io.IOException;
|
|
23
|
|
-import java.io.InputStream;
|
|
24
|
|
-import java.io.OutputStream;
|
|
25
|
|
-import java.lang.ref.WeakReference;
|
|
26
|
|
-import java.net.Socket;
|
|
27
|
|
-import java.util.Arrays;
|
|
28
|
|
-
|
|
29
|
|
-public class SocketService extends Service {
|
|
30
|
|
- private static final String TAG = "BackService";
|
|
31
|
|
- private static final long HEART_BEAT_RATE = 5 * 1000;
|
|
32
|
|
-
|
|
33
|
|
- public static final int JUST_OPEN_DOOR = 1701;
|
|
34
|
|
- public static final int USER_OPEN_DOOR_AND_GET_MOVIE = 1703;
|
|
35
|
|
- public static final int USER_OPEN_DOOR = 1704;//get the movie again charge show new movie
|
|
36
|
|
- // public static final int USER_OPEN_DOOR_AND_GET_MOVIE = 1703;
|
|
37
|
|
- /*
|
|
38
|
|
- * 10001 //服务器发送了命令(带命令的返回消息),这个一般是服务器主动发消息的时候code为这个值
|
|
39
|
|
- 10002 //连接(认证)失败
|
|
40
|
|
- 10003 //还未注册连接就进行认证
|
|
41
|
|
- 10004 //终端编号未传
|
|
42
|
|
- 10005 //心跳异常,没有找到房间连接信息
|
|
43
|
|
- 10006 //心跳异常,连接标识符不一致
|
|
44
|
|
- 10007 //收到未知命令
|
|
45
|
|
- 10008 //收到无法解析的消息
|
|
46
|
|
- 10009 //已经认证过了,不允许再认证
|
|
47
|
|
- 10010 //room_sn 已连接,不能再连接
|
|
48
|
|
- */
|
|
49
|
|
- private static final int VERIFY_SUCCESS = 9999;
|
|
50
|
|
- private static final int SUCCESS_MESSAGE = 10000;
|
|
51
|
|
- private static final int CONTAIN_MESSAGE = 10001;
|
|
52
|
|
- private static final int AUTHENTICATION_FAILED = 10002; //连接(认证)失败
|
|
53
|
|
- private static final int CONNECTION_BEFORE_AUTHENTICATION = 10003;//还未注册连接就进行认证
|
|
54
|
|
- private static final int NOT_SEND_ROOM_SN = 10004;//终端编号未传
|
|
55
|
|
- private static final int HEART_BEAR_ERROR = 10005; //心跳异常,没有找到房间连接信息
|
|
56
|
|
- private static final int HEART_BEAR_ERROR_SYMBOL = 10006;//心跳异常,连接标识符不一致
|
|
57
|
|
- private static final int OPEN_DOOR = 50001;//开门命令
|
|
58
|
|
- private static final int CLEAN_OVER = 50002;//
|
|
59
|
|
-
|
|
60
|
|
- private static final int ROOM_HAS_REGISTERED = 1001;
|
|
61
|
|
- private static final int ROOM_NOT_EXIST_M = 1002;
|
|
62
|
|
- private static final int HEAT_BEAT_RTN = 1003;
|
|
63
|
|
-
|
|
64
|
|
- // online
|
|
65
|
|
- public static final String SERVER_HOST_ONLINE = "conn.ft.qnbar.com";// "192.168.1.21";//
|
|
66
|
|
- public static final int SERVER_PORT_ONLINE = 8899;
|
|
67
|
|
-
|
|
68
|
|
- // develop
|
|
69
|
|
- public static final String SERVER_HOST_DEVELOP = "10.10.4.6";// "192.168.1.21";//
|
|
70
|
|
- public static final int SERVER_PORT_DEVELOP = 9501;
|
|
71
|
|
- // test
|
|
72
|
|
- public static final String SERVER_HOST = "10.10.4.6";// "192.168.1.21";//
|
|
73
|
|
- public static final int SERVER_PORT = 8899;
|
|
74
|
|
-
|
|
75
|
|
- private String serverHost = SERVER_HOST;
|
|
76
|
|
- private int serverPort = SERVER_PORT;
|
|
77
|
|
-
|
|
78
|
|
-
|
|
79
|
|
- public static final String END_SYMBOL = "\\r\\n\\r\\n";//心跳包内容
|
|
80
|
|
- // public String testRoomSn = "R170413034374";
|
|
81
|
|
- public String testRoomSn = "c";
|
|
82
|
|
-
|
|
83
|
|
- private ReadThread mReadThread;
|
|
84
|
|
- private HeatBeatThread mHeatBeatThread;
|
|
85
|
|
- private Gson gson;
|
|
86
|
|
-
|
|
87
|
|
-// private LocalBroadcastManager mLocalBroadcastManager;
|
|
88
|
|
-
|
|
89
|
|
- private WeakReference<Socket> mSocket;
|
|
90
|
|
-
|
|
91
|
|
- // For heart Beat
|
|
92
|
|
-// private Handler mHandler = new Handler();
|
|
93
|
|
-// private Runnable heartBeatRunnable = new Runnable() {
|
|
94
|
|
-//
|
|
95
|
|
-// @Override
|
|
96
|
|
-// public void run() {
|
|
97
|
|
-// if (System.currentTimeMillis() - sendTime >= HEART_BEAT_RATE) {
|
|
98
|
|
-// Log.d(TAG, "heart beat");
|
|
99
|
|
-// boolean isSuccess = sendMsg(new Gson().toJson(new SocketSendMsg().contractHeartBeatMsg(testRoomSn)) + END_SYMBOL);//就发送一个HEART_BEAT_STRING过去 如果发送失败,就重新初始化一个socket
|
|
100
|
|
-// if (!isSuccess) {
|
|
101
|
|
-// Log.d(TAG, "heart beat error restart");
|
|
102
|
|
-// mHandler.removeCallbacks(heartBeatRunnable);
|
|
103
|
|
-// mReadThread.release();
|
|
104
|
|
-// mHeatBeatThread.release();
|
|
105
|
|
-// releaseLastSocket(mSocket);
|
|
106
|
|
-// new InitSocketThread().start();
|
|
107
|
|
-// }
|
|
108
|
|
-// } else {
|
|
109
|
|
-// Log.d(TAG, "heart beat less than beat rate");
|
|
110
|
|
-// }
|
|
111
|
|
-// mHandler.postDelayed(this, HEART_BEAT_RATE);
|
|
112
|
|
-// }
|
|
113
|
|
-// };
|
|
114
|
|
-
|
|
115
|
|
- private long sendTime = 0L;
|
|
116
|
|
- private Context context;
|
|
117
|
|
-
|
|
118
|
|
- @Override
|
|
119
|
|
- public IBinder onBind(Intent arg0) {
|
|
120
|
|
- return null;
|
|
121
|
|
- }
|
|
122
|
|
-
|
|
123
|
|
- @Override
|
|
124
|
|
- public void onCreate() {
|
|
125
|
|
- super.onCreate();
|
|
126
|
|
- gson = new Gson();
|
|
127
|
|
- context = this;
|
|
128
|
|
- }
|
|
129
|
|
-
|
|
130
|
|
- @Override
|
|
131
|
|
- public int onStartCommand(Intent intent, int flags, int startId) {
|
|
132
|
|
- int ftTest = Utils.getInt(this, "ft-test", 0);
|
|
133
|
|
- switch (ftTest) {
|
|
134
|
|
- case 0:
|
|
135
|
|
- serverHost = SERVER_HOST_ONLINE;
|
|
136
|
|
- serverPort = SERVER_PORT_ONLINE;
|
|
137
|
|
- break;
|
|
138
|
|
- case 1:
|
|
139
|
|
- serverHost = SERVER_HOST_DEVELOP;
|
|
140
|
|
- serverPort = SERVER_PORT_DEVELOP;
|
|
141
|
|
- break;
|
|
142
|
|
- case 2:
|
|
143
|
|
- serverHost = SERVER_HOST;
|
|
144
|
|
- serverPort = SERVER_PORT;
|
|
145
|
|
- break;
|
|
146
|
|
- }
|
|
147
|
|
-
|
|
148
|
|
- String roomInfoStr = Utils.getString(this, "room-info");
|
|
149
|
|
- if (!TextUtils.isEmpty(roomInfoStr)) {
|
|
150
|
|
- Log.d("room-info", "room info not null");
|
|
151
|
|
- RoomInfo roomInfo = null;
|
|
152
|
|
- try {
|
|
153
|
|
- roomInfo = gson.fromJson(roomInfoStr, RoomInfo.class);
|
|
154
|
|
- Log.d("room-info", "room info room_sn update");
|
|
155
|
|
- testRoomSn = roomInfo.getData().getRoom_sn();
|
|
156
|
|
- } catch (JsonSyntaxException e) {
|
|
157
|
|
- Log.d("room-info", "room gson parse exception return");
|
|
158
|
|
- Toast.makeText(this, "房间信息配置出错,请重新进入应用获取", Toast.LENGTH_SHORT).show();
|
|
159
|
|
- e.printStackTrace();
|
|
160
|
|
- return super.onStartCommand(intent, flags, startId);
|
|
161
|
|
- }
|
|
162
|
|
- } else {
|
|
163
|
|
- Log.d("room-info", "room info not exist");
|
|
164
|
|
- Toast.makeText(this, "没有获取到房间信息,请查看后台配置", Toast.LENGTH_SHORT).show();
|
|
165
|
|
- return super.onStartCommand(intent, flags, startId);
|
|
166
|
|
- }
|
|
167
|
|
-// }
|
|
168
|
|
- new InitSocketThread().start();
|
|
169
|
|
- Log.d(TAG, "socket service onCreate");
|
|
170
|
|
- return super.onStartCommand(intent, flags, startId);
|
|
171
|
|
- }
|
|
172
|
|
-
|
|
173
|
|
- public boolean sendMsg(String msg) {
|
|
174
|
|
- if (null == mSocket || null == mSocket.get()) {
|
|
175
|
|
- return false;
|
|
176
|
|
- }
|
|
177
|
|
- Log.d(TAG, "send msg:" + msg);
|
|
178
|
|
- Socket soc = mSocket.get();
|
|
179
|
|
- try {
|
|
180
|
|
- if (!soc.isClosed() && !soc.isOutputShutdown()) {
|
|
181
|
|
- OutputStream os = soc.getOutputStream();
|
|
182
|
|
- String message = msg;
|
|
183
|
|
- os.write(message.getBytes());
|
|
184
|
|
- os.flush();
|
|
185
|
|
- sendTime = System.currentTimeMillis();//每次发送成数据,就改一下最后成功发送的时间,节省心跳间隔时间
|
|
186
|
|
- } else {
|
|
187
|
|
- return false;
|
|
188
|
|
- }
|
|
189
|
|
- } catch (IOException e) {
|
|
190
|
|
- e.printStackTrace();
|
|
191
|
|
- return false;
|
|
192
|
|
- }
|
|
193
|
|
- return true;
|
|
194
|
|
- }
|
|
195
|
|
-
|
|
196
|
|
- private void initSocket() throws IOException {//初始化Socket
|
|
197
|
|
- Log.d(TAG, "serverHost:serverPort:" + serverHost + ":" + serverPort);
|
|
198
|
|
- Socket so = new Socket(serverHost, serverPort);
|
|
199
|
|
- mSocket = new WeakReference<Socket>(so);
|
|
200
|
|
- mReadThread = new ReadThread(so);
|
|
201
|
|
- mReadThread.start();
|
|
202
|
|
-// new Thread(heartBeatRunnable).start();
|
|
203
|
|
-// mHandler.post(heartBeatRunnable);//初始化成功后,就准备发送心跳包
|
|
204
|
|
- mHeatBeatThread = new HeatBeatThread(so);
|
|
205
|
|
-// mHeatBeatThread.start();//上面的 one plus 3t NetworkOnMainThreadException!!!
|
|
206
|
|
- }
|
|
207
|
|
-
|
|
208
|
|
-
|
|
209
|
|
- private void releaseLastSocket(WeakReference<Socket> mSocket) {
|
|
210
|
|
- try {
|
|
211
|
|
- if (null != mSocket) {
|
|
212
|
|
- Socket sk = mSocket.get();
|
|
213
|
|
- if (!sk.isClosed()) {
|
|
214
|
|
- sk.close();
|
|
215
|
|
- }
|
|
216
|
|
- sk = null;
|
|
217
|
|
- mSocket = null;
|
|
218
|
|
- }
|
|
219
|
|
- } catch (IOException e) {
|
|
220
|
|
- e.printStackTrace();
|
|
221
|
|
- }
|
|
222
|
|
- }
|
|
223
|
|
-
|
|
224
|
|
- class InitSocketThread extends Thread {
|
|
225
|
|
- @Override
|
|
226
|
|
- public void run() {
|
|
227
|
|
- super.run();
|
|
228
|
|
- try {
|
|
229
|
|
- initSocket();
|
|
230
|
|
- } catch (IOException e) {
|
|
231
|
|
- e.printStackTrace();
|
|
232
|
|
- Log.d(TAG, "init socket thread error,restart again after 10's");
|
|
233
|
|
- try {
|
|
234
|
|
- Thread.sleep(HEART_BEAT_RATE);
|
|
235
|
|
- } catch (InterruptedException e1) {
|
|
236
|
|
- e1.printStackTrace();
|
|
237
|
|
- }
|
|
238
|
|
- this.run();
|
|
239
|
|
- }
|
|
240
|
|
- }
|
|
241
|
|
- }
|
|
242
|
|
-
|
|
243
|
|
- private String varifyMsg = "";
|
|
244
|
|
-
|
|
245
|
|
- // Thread to read content from Socket
|
|
246
|
|
- class ReadThread extends Thread {
|
|
247
|
|
- private WeakReference<Socket> mWeakSocket;
|
|
248
|
|
- private boolean isStart = true;
|
|
249
|
|
-
|
|
250
|
|
- public ReadThread(Socket socket) {
|
|
251
|
|
- mWeakSocket = new WeakReference<Socket>(socket);
|
|
252
|
|
- }
|
|
253
|
|
-
|
|
254
|
|
- public void release() {
|
|
255
|
|
- isStart = false;
|
|
256
|
|
- releaseLastSocket(mWeakSocket);
|
|
257
|
|
- }
|
|
258
|
|
-
|
|
259
|
|
- @Override
|
|
260
|
|
- public void run() {
|
|
261
|
|
- super.run();
|
|
262
|
|
- Socket socket = mWeakSocket.get();
|
|
263
|
|
- if (null != socket) {
|
|
264
|
|
- try {
|
|
265
|
|
- if (!sendRegister) {
|
|
266
|
|
- Log.d(TAG, "send register mes");
|
|
267
|
|
- SocketSendMsg ssm = new SocketSendMsg().contractRegisterMsg(testRoomSn);
|
|
268
|
|
- String msg = gson.toJson(ssm) + END_SYMBOL;
|
|
269
|
|
- sendMsg(msg);
|
|
270
|
|
- Log.d(TAG, "" + msg);
|
|
271
|
|
- sendRegister = true;
|
|
272
|
|
- }
|
|
273
|
|
- Log.d(TAG, "send register mes end");
|
|
274
|
|
- InputStream is = socket.getInputStream();
|
|
275
|
|
- byte[] buffer = new byte[1024 * 4];
|
|
276
|
|
- int length = 0;
|
|
277
|
|
- while (!socket.isClosed() && !socket.isInputShutdown()
|
|
278
|
|
- && isStart && ((length = is.read(buffer)) != -1)) {
|
|
279
|
|
- if (length > 0) {
|
|
280
|
|
- String message = new String(Arrays.copyOf(buffer,
|
|
281
|
|
- length)).trim();
|
|
282
|
|
- Log.d(TAG, "end:" + message.contains(END_SYMBOL) + "");
|
|
283
|
|
- Log.d(TAG, "recv msg:" + message);
|
|
284
|
|
- try {
|
|
285
|
|
- if (message.endsWith(END_SYMBOL)) {
|
|
286
|
|
- message = message.replace(END_SYMBOL, "");
|
|
287
|
|
- }
|
|
288
|
|
- SocketResponse socketResponse = gson.fromJson(message/*.substring(0, message.length() - 8)*/, SocketResponse.class);
|
|
289
|
|
- switch (socketResponse.getCode()) {
|
|
290
|
|
- case SUCCESS_MESSAGE:
|
|
291
|
|
- Log.d(TAG, "success:" + socketResponse.getCmd());
|
|
292
|
|
- if (!TextUtils.isEmpty(socketResponse.getData().getVerify())) {
|
|
293
|
|
- varifyMsg = AuthCode.getDecodeStr(socketResponse.getData().getVerify());
|
|
294
|
|
- SocketSendMsg ssm = new SocketSendMsg().contractVerifyMsg(testRoomSn, varifyMsg);
|
|
295
|
|
- String msg = gson.toJson(ssm) + END_SYMBOL;
|
|
296
|
|
- sendMsg(msg);
|
|
297
|
|
- }
|
|
298
|
|
- break;
|
|
299
|
|
- case VERIFY_SUCCESS:
|
|
300
|
|
- Log.d(TAG, "VERIFY_SUCCESS:" + socketResponse.getCmd());
|
|
301
|
|
- mHeatBeatThread.start();//上面的 one plus 3t NetworkOnMainThreadException!!!
|
|
302
|
|
- Log.d(TAG, "verify success start heart beat");
|
|
303
|
|
- break;
|
|
304
|
|
- case CONTAIN_MESSAGE:
|
|
305
|
|
- if (socketResponse.getData() != null) {
|
|
306
|
|
- if (socketResponse.getCmd() == OPEN_DOOR) {
|
|
307
|
|
-// new SystemUtils().openFtLed(context.getApplicationContext());
|
|
308
|
|
- switch (socketResponse.getData().getUser()) {
|
|
309
|
|
- //10用户,20管理员,默认值为0
|
|
310
|
|
- case 10:
|
|
311
|
|
- new SystemUtils().openFtLed(context.getApplicationContext());
|
|
312
|
|
- if (TextUtils.isEmpty(((FangTangApplication) getApplication()).getCurrentPlayUrl())) {
|
|
313
|
|
- LightOperationUtils.open();
|
|
314
|
|
- LightOperationUtils.setLightValue(Utils.getInt(context, "brightness", 50));
|
|
315
|
|
- }
|
|
316
|
|
- sendMessage(QrCodeShowActivity.KILL_SELF, "finish the QR CODE activity when new user come in");
|
|
317
|
|
- if (socketResponse.getData().getFirst() == 1) {
|
|
318
|
|
-// LightOperationUtils.open();
|
|
319
|
|
-// LightOperationUtils.setLightValue(Utils.getInt(context, "brightness", 50));
|
|
320
|
|
- sendMessage(USER_OPEN_DOOR_AND_GET_MOVIE, "user first open the door");
|
|
321
|
|
- } else {
|
|
322
|
|
- sendMessage(USER_OPEN_DOOR, "user open the door");
|
|
323
|
|
- }
|
|
324
|
|
- break;
|
|
325
|
|
- case 20:
|
|
326
|
|
- if (TextUtils.isEmpty(((FangTangApplication) getApplication()).getCurrentPlayUrl())) {
|
|
327
|
|
- Log.d("LightOperationUtils", "admin open light");
|
|
328
|
|
- LightOperationUtils.open();
|
|
329
|
|
- LightOperationUtils.setLightValue(Utils.getInt(context, "brightness", 50));
|
|
330
|
|
- }
|
|
331
|
|
- sendMessage(JUST_OPEN_DOOR, "administrator open the door");
|
|
332
|
|
- break;
|
|
333
|
|
- case 0:
|
|
334
|
|
- sendMessage(JUST_OPEN_DOOR, "get zero none user or administrator open the door");
|
|
335
|
|
- break;
|
|
336
|
|
- default:
|
|
337
|
|
- sendMessage(JUST_OPEN_DOOR, "none user or administrator open the door");
|
|
338
|
|
- break;
|
|
339
|
|
- }
|
|
340
|
|
- } else if (socketResponse.getCmd() == CLEAN_OVER) {
|
|
341
|
|
- Log.d("LightOperationUtils", "admin clean over close light");
|
|
342
|
|
- LightOperationUtils.setLightValue(5);
|
|
343
|
|
- LightOperationUtils.close();
|
|
344
|
|
- new SystemUtils().closeFtLed(context.getApplicationContext());
|
|
345
|
|
- sendMessage(QrCodeShowActivity.KILL_SELF, "finish the QR CODE activity when clean over");
|
|
346
|
|
- }
|
|
347
|
|
- }
|
|
348
|
|
- break;
|
|
349
|
|
- default:
|
|
350
|
|
- Log.d(TAG, "print msg:" + socketResponse.toString());
|
|
351
|
|
- }
|
|
352
|
|
-// if (("sendMessage").equals(socketResponse.getCmd())) {
|
|
353
|
|
-// MessageEvent messageEvent = new MessageEvent();
|
|
354
|
|
-// messageEvent.setEventId(JUST_OPEN_DOOR);
|
|
355
|
|
-// messageEvent.setMessage("click item");
|
|
356
|
|
-// EventBus.getDefault().post(messageEvent);
|
|
357
|
|
-// }
|
|
358
|
|
- } catch (JsonSyntaxException e) {
|
|
359
|
|
- Log.d(TAG, message);
|
|
360
|
|
- e.printStackTrace();
|
|
361
|
|
- }
|
|
362
|
|
- //收到服务器过来的消息,就通过Broadcast发送出去
|
|
363
|
|
-// if (message.equals(END_SYMBOL)) {//处理心跳回复
|
|
364
|
|
-// Intent intent = new Intent(HEART_BEAT_ACTION);
|
|
365
|
|
-// mLocalBroadcastManager.sendBroadcast(intent);
|
|
366
|
|
-// } else {
|
|
367
|
|
-// //其他消息回复
|
|
368
|
|
-// Intent intent = new Intent(MESSAGE_ACTION);
|
|
369
|
|
-// intent.putExtra("message", message);
|
|
370
|
|
-// mLocalBroadcastManager.sendBroadcast(intent);
|
|
371
|
|
-// }
|
|
372
|
|
- }
|
|
373
|
|
- }
|
|
374
|
|
- } catch (IOException e) {
|
|
375
|
|
- e.printStackTrace();
|
|
376
|
|
- }
|
|
377
|
|
- }
|
|
378
|
|
- }
|
|
379
|
|
- }
|
|
380
|
|
-
|
|
381
|
|
- private void sendMessage(int type, String msg) {
|
|
382
|
|
- MessageEvent messageEvent = new MessageEvent();
|
|
383
|
|
- messageEvent.setEventId(type);
|
|
384
|
|
- messageEvent.setMessage(msg);
|
|
385
|
|
- EventBus.getDefault().post(messageEvent);
|
|
386
|
|
- }
|
|
387
|
|
-
|
|
388
|
|
- boolean sendRegister = false;
|
|
389
|
|
-
|
|
390
|
|
-
|
|
391
|
|
- // Thread to read content from Socket
|
|
392
|
|
- class HeatBeatThread extends Thread {
|
|
393
|
|
- private WeakReference<Socket> mWeakSocket;
|
|
394
|
|
- private boolean isStart = true;
|
|
395
|
|
-
|
|
396
|
|
- public HeatBeatThread(Socket socket) {
|
|
397
|
|
- mWeakSocket = new WeakReference<Socket>(socket);
|
|
398
|
|
- }
|
|
399
|
|
-
|
|
400
|
|
- public void release() {
|
|
401
|
|
- isStart = false;
|
|
402
|
|
- releaseLastSocket(mWeakSocket);
|
|
403
|
|
- }
|
|
404
|
|
-
|
|
405
|
|
- @Override
|
|
406
|
|
- public void run() {
|
|
407
|
|
- super.run();
|
|
408
|
|
- Socket socket = mWeakSocket.get();
|
|
409
|
|
- if (null != socket) {
|
|
410
|
|
- while (isStart) {
|
|
411
|
|
- if (System.currentTimeMillis() - sendTime >= HEART_BEAT_RATE) {
|
|
412
|
|
- Log.d(TAG, "heart beat:" + Thread.currentThread().getId());
|
|
413
|
|
- boolean isSuccess = sendMsg(new Gson().toJson(new SocketSendMsg().contractHeartBeatMsg(testRoomSn)) + END_SYMBOL);//就发送一个HEART_BEAT_STRING过去 如果发送失败,就重新初始化一个socket
|
|
414
|
|
- if (!isSuccess) {
|
|
415
|
|
- Log.d(TAG, "heart beat error restart:" + Thread.currentThread().getId());
|
|
416
|
|
-// mHandler.removeCallbacks(heartBeatRunnable);
|
|
417
|
|
- mReadThread.release();
|
|
418
|
|
- mHeatBeatThread.release();
|
|
419
|
|
- sendRegister = false;
|
|
420
|
|
- releaseLastSocket(mSocket);
|
|
421
|
|
- new InitSocketThread().start();
|
|
422
|
|
- }
|
|
423
|
|
- } else {
|
|
424
|
|
- Log.d(TAG, "heart beat less than beat rate:" + Thread.currentThread().getId());
|
|
425
|
|
- }
|
|
426
|
|
- try {
|
|
427
|
|
- Thread.sleep(HEART_BEAT_RATE);
|
|
428
|
|
- } catch (InterruptedException e) {
|
|
429
|
|
- e.printStackTrace();
|
|
430
|
|
- }
|
|
431
|
|
- }
|
|
432
|
|
- }
|
|
433
|
|
- }
|
|
434
|
|
- }
|
|
435
|
|
-
|
|
436
|
|
- @Override
|
|
437
|
|
- public void onDestroy() {
|
|
438
|
|
- Log.d(TAG, "socket service destroy");
|
|
439
|
|
- super.onDestroy();
|
|
440
|
|
- }
|
|
441
|
|
-} |
|
|
\ No newline at end of file |