-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBackServicecocket.java
264 lines (264 loc) · 8.47 KB
/
BackServicecocket.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
//package com.hbrc.srcapp.Receiver;
//
//import android.app.Service;
//import android.content.Intent;
//import android.os.Handler;
//import android.os.IBinder;
//import android.os.RemoteException;
//import android.support.v4.content.LocalBroadcastManager;
//import android.util.Log;
//
//import java.io.IOException;
//import java.io.InputStream;
//import java.io.OutputStream;
//import java.lang.ref.WeakReference;
//import java.net.Socket;
//import java.net.UnknownHostException;
//import java.util.Arrays;
//
//import okhttp3.OkHttpClient;
//import okhttp3.Request;
//
///**
// * Created by 王学波 on 2019/1/22.
// */
//public class BackServicecocket extends Service {
//
// private static final String TAG = "danxx";
// /**
// * 心跳频率
// */
// private static final long HEART_BEAT_RATE = 3 * 1000;
// /**
// * 服务器ip地址
// */
// // public static final String HOST = "192.168.123.27";// "192.168.1.21";//
// public static String HOST = "";
//
// /**
// * 服务器端口号
// */
// public static int PORT = 8080;
// /**
// * 服务器端口号
// */
// public static String TOKEN = "";
// /**
// * 服务器消息回复广播
// */
// public static final String MESSAGE_ACTION = "message_ACTION";
// /**
// * 服务器心跳回复广播
// */
// public static final String HEART_BEAT_ACTION = "heart_beat_ACTION";
// /**
// * 读线程
// */
// private ReadThread mReadThread;
//
// private LocalBroadcastManager mLocalBroadcastManager;
// /***/
// private WeakReference<Socket> mSocket;
//
// // For heart Beat
// private Handler mHandler = new Handler();
// /**
// * 心跳任务,不断重复调用自己
// */
// private Runnable heartBeatRunnable = new Runnable() {
//
// @Override
// public void run() {
// if (System.currentTimeMillis() - sendTime >= HEART_BEAT_RATE) {
// boolean isSuccess = sendMsg("HeartBeat");//就发送一个\r\n过去 如果发送失败,就重新初始化一个socket
// if (!isSuccess) {
// mHandler.removeCallbacks(heartBeatRunnable);
// mReadThread.release();
// releaseLastSocket(mSocket);
// new InitSocketThread().start();
// }
// }
// mHandler.postDelayed(this, HEART_BEAT_RATE);
// }
// };
//
// private long sendTime = 0L;
// /**
// * aidl通讯回调
// */
// private IBackService.Stub iBackService = new IBackService.Stub() {
//
// /**
// * 收到内容发送消息
// *
// * @param message 需要发送到服务器的消息
// * @return
// * @throws RemoteException
// */
// @Override
// public boolean sendMessage(String message) throws RemoteException {
// return sendMsg(message);
// }
// };
//
// @Override
// public IBinder onBind(Intent arg0) {
// return iBackService;
// }
//
// @Override
// public void onCreate() {
// super.onCreate();
// new InitSocketThread().start();
// mLocalBroadcastManager = LocalBroadcastManager.getInstance(this);
//
// }
//
// public boolean sendMsg(final String msg) {
// if (null == mSocket || null == mSocket.get()) {
// return false;
// }
// final Socket soc = mSocket.get();
// if (!soc.isClosed() && !soc.isOutputShutdown()) {
// new Thread(new Runnable() {
// @Override
// public void run() {
// try {
// OutputStream os = soc.getOutputStream();
// String message = msg + "\r\n";
// os.write(message.getBytes());
// os.flush();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }).start();
// sendTime = System.currentTimeMillis();//每次发送成数据,就改一下最后成功发送的时间,节省心跳间隔时间
// } else {
// return false;
// }
// return true;
// }
// private void connect() {
//
// EchoWebSocketListener listener = new EchoWebSocketListener();
// Request request = new Request.Builder()
// .url("ws://"+HOST+":"+PORT)
// .build();
// OkHttpClient client = new OkHttpClient();
// client.newWebSocket(request, listener);
//
// client.dispatcher().executorService().shutdown();
// }
//
// private void initSocket() {//初始化Socket
// try {
//
// Socket so = new Socket(HOST, PORT);
//
// mSocket = new WeakReference<Socket>(so);
//// try {
//// final Socket soc = mSocket.get();
//// OutputStream os = soc.getOutputStream();
//// String message = "token="+TOKEN;
//// os.write(message.getBytes());
//// os.flush();
//// }catch (Exception e){
//// e.printStackTrace();
//// }
// mReadThread = new ReadThread(so);
// mReadThread.start();
// mHandler.postDelayed(heartBeatRunnable, HEART_BEAT_RATE);//初始化成功后,就准备发送心跳包
// } catch (UnknownHostException e) {
// e.printStackTrace();
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
//
// /**
// * 心跳机制判断出socket已经断开后,就销毁连接方便重新创建连接
// *
// * @param mSocket
// */
// private void releaseLastSocket(WeakReference<Socket> mSocket) {
// try {
// if (null != mSocket) {
// Socket sk = mSocket.get();
// if (!sk.isClosed()) {
// sk.close();
// }
// sk = null;
// mSocket = null;
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
//
// class InitSocketThread extends Thread {
// @Override
// public void run() {
// super.run();
// initSocket();
// }
// }
//
// // Thread to read content from Socket
// class ReadThread extends Thread {
// private WeakReference<Socket> mWeakSocket;
// private boolean isStart = true;
//
// public ReadThread(Socket socket) {
// mWeakSocket = new WeakReference<Socket>(socket);
// }
//
// public void release() {
// isStart = false;
// releaseLastSocket(mWeakSocket);
// }
//
// @Override
// public void run() {
// super.run();
// Socket socket = mWeakSocket.get();
// if (null != socket) {
// try {
// InputStream is = socket.getInputStream();
// byte[] buffer = new byte[1024 * 4];
// int length = 0;
// while (!socket.isClosed() && !socket.isInputShutdown()
// && isStart && ((length = is.read(buffer)) != -1)) {
// if (length > 0) {
// String message = new String(Arrays.copyOf(buffer,
// length)).trim();
// Log.e(TAG, message);
// //收到服务器过来的消息,就通过Broadcast发送出去
// if (message.equals("ok")) {//处理心跳回复
// Intent intent = new Intent(HEART_BEAT_ACTION);
// mLocalBroadcastManager.sendBroadcast(intent);
// } else {
// //其他消息回复
// Intent intent = new Intent(MESSAGE_ACTION);
// intent.putExtra("message", message);
// mLocalBroadcastManager.sendBroadcast(intent);
// }
// }
// }
// } catch (IOException e) {
// e.printStackTrace();
// }
// }
// }
// }
//
// @Override
// public void onDestroy() {
// super.onDestroy();
// mHandler.removeCallbacks(heartBeatRunnable);
// mReadThread.release();
// releaseLastSocket(mSocket);
// }
//
//
//}