update:2019.04.11
fix:增加在线状态 add:
This commit is contained in:
@@ -15,8 +15,8 @@ android {
|
||||
applicationId "com.info.sn"
|
||||
minSdkVersion 23
|
||||
targetSdkVersion 28
|
||||
versionCode 16
|
||||
versionName "1.1.6"
|
||||
versionCode 17
|
||||
versionName "1.1.7"
|
||||
multiDexEnabled true
|
||||
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
|
||||
ndk {
|
||||
@@ -120,6 +120,7 @@ dependencies {
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
|
||||
|
||||
implementation 'com.google.zxing:core:3.2.1'
|
||||
|
||||
implementation 'cn.jiguang.sdk:jpush:3.3.4' // 此处以JPush 3.3.4 版本为例。
|
||||
implementation 'cn.jiguang.sdk:jcore:2.1.2' // 此处以JCore 2.1.2 版本为例。
|
||||
//动态权限框架
|
||||
@@ -128,8 +129,13 @@ dependencies {
|
||||
implementation 'com.lzy.net:okgo:3.0.4'
|
||||
implementation 'com.lzy.net:okrx:0.1.2'
|
||||
implementation 'com.lzy.net:okserver:2.0.5'
|
||||
|
||||
implementation 'com.alibaba:fastjson:1.2.21'
|
||||
|
||||
// implementation 'com.blankj:utilcode:1.26.0'
|
||||
implementation 'com.arialyy.aria:core:3.7.10'
|
||||
annotationProcessor 'com.arialyy.aria:compiler:3.7.10'
|
||||
|
||||
implementation "org.java-websocket:Java-WebSocket:1.4.1"
|
||||
|
||||
}
|
||||
|
||||
36
app/src/main/java/com/info/sn/service/JWebSocketClient.java
Normal file
36
app/src/main/java/com/info/sn/service/JWebSocketClient.java
Normal file
@@ -0,0 +1,36 @@
|
||||
package com.info.sn.service;
|
||||
|
||||
import android.util.Log;
|
||||
|
||||
import org.java_websocket.client.WebSocketClient;
|
||||
import org.java_websocket.drafts.Draft_6455;
|
||||
import org.java_websocket.handshake.ServerHandshake;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
public class JWebSocketClient extends WebSocketClient {
|
||||
public JWebSocketClient(URI serverUri) {
|
||||
super(serverUri, new Draft_6455());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOpen(ServerHandshake handshakedata) {
|
||||
Log.i("JWebSocketClient", "onOpen()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onMessage(String message) {
|
||||
Log.i("JWebSocketClient", "onMessage()");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClose(int code, String reason, boolean remote) {
|
||||
Log.i("JWebSocketClient", "onClose():" + reason);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onError(Exception ex) {
|
||||
Log.i("JWebSocketClient", "onError():" + ex.getMessage());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -12,13 +12,21 @@ import android.content.ComponentName;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.ServiceConnection;
|
||||
import android.os.Binder;
|
||||
import android.os.Handler;
|
||||
import android.os.IBinder;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.Nullable;
|
||||
|
||||
import com.info.sn.KeepAliveConnection;
|
||||
import com.info.sn.utils.LogUtils;
|
||||
import com.info.sn.utils.ServiceAliveUtils;
|
||||
import com.info.sn.utils.Utils;
|
||||
|
||||
import org.java_websocket.handshake.ServerHandshake;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
|
||||
/**
|
||||
@@ -28,8 +36,17 @@ import com.info.sn.utils.ServiceAliveUtils;
|
||||
* @time Created by 2018/8/17 11:26
|
||||
*/
|
||||
public class StepService extends Service {
|
||||
|
||||
public JWebSocketClient client;
|
||||
private JWebSocketClientBinder mBinder = new JWebSocketClientBinder();
|
||||
private final static String TAG = StepService.class.getSimpleName();
|
||||
|
||||
//用于Activity和service通讯
|
||||
public class JWebSocketClientBinder extends Binder {
|
||||
public StepService getService() {
|
||||
return StepService.this;
|
||||
}
|
||||
}
|
||||
|
||||
private ServiceConnection mServiceConnection = new ServiceConnection() {
|
||||
@Override
|
||||
public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
|
||||
@@ -62,7 +79,121 @@ public class StepService extends Service {
|
||||
// startForeground(1, new Notification());
|
||||
// 绑定建立链接
|
||||
bindService(new Intent(this, GuardService.class), mServiceConnection, Context.BIND_IMPORTANT);
|
||||
//初始化websocket
|
||||
initSocketClient();
|
||||
mHandler.postDelayed(heartBeatRunnable, HEART_BEAT_RATE);//开启心跳检测
|
||||
return START_STICKY;
|
||||
}
|
||||
|
||||
/**
|
||||
* 初始化websocket连接
|
||||
*/
|
||||
private void initSocketClient() {
|
||||
URI uri = URI.create("ws://47.107.116.173:2345");
|
||||
// URI uri = URI.create("ws://echo.websocket.org");
|
||||
// URI uri = URI.create("ws://123.207.136.134:9010/ajaxchattest");
|
||||
client = new JWebSocketClient(uri) {
|
||||
@Override
|
||||
public void onMessage(String message) {
|
||||
Log.i("JWebSocketClientService", "收到服务器发来的消息:" + message + "?");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onOpen(ServerHandshake handshakedata) {
|
||||
super.onOpen(handshakedata);
|
||||
Log.i("JWebSocketClientService", "websocket连接成功");
|
||||
}
|
||||
};
|
||||
connect();
|
||||
}
|
||||
|
||||
/**
|
||||
* 连接websocket
|
||||
*/
|
||||
private void connect() {
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
//connectBlocking多出一个等待操作,会先连接再发送,否则未连接发送会报错
|
||||
Log.i("JWebSocketClientService", "websocket链接中");
|
||||
client.connectBlocking();
|
||||
} catch (InterruptedException e) {
|
||||
Log.i("JWebSocketClientService", e.getMessage());
|
||||
// e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 发送消息
|
||||
*
|
||||
* @param msg
|
||||
*/
|
||||
public void sendMsg(String msg) {
|
||||
if (null != client) {
|
||||
Log.i("JWebSocketClientService", "发送的消息:" + msg);
|
||||
client.send(msg);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 断开连接
|
||||
*/
|
||||
private void closeConnect() {
|
||||
try {
|
||||
if (null != client) {
|
||||
client.close();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
} finally {
|
||||
client = null;
|
||||
}
|
||||
}
|
||||
|
||||
// -------------------------------------websocket心跳检测------------------------------------------------
|
||||
private static final long HEART_BEAT_RATE = 50 * 1000;//每隔55秒进行一次对长连接的心跳检测
|
||||
private Handler mHandler = new Handler();
|
||||
private Runnable heartBeatRunnable = new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
Log.i("JWebSocketClientService", "心跳包检测websocket连接状态");
|
||||
if (client != null) {
|
||||
if (client.isOpen()) {
|
||||
Log.i("JWebSocketClientService", "websocket已连接");
|
||||
sendMsg(Utils.getSerial());
|
||||
} else if (client.isClosed()) {
|
||||
Log.i("JWebSocketClientService", "websocket重连中");
|
||||
reconnectWs();
|
||||
}
|
||||
} else {
|
||||
//如果client已为空,重新初始化连接
|
||||
client = null;
|
||||
initSocketClient();
|
||||
}
|
||||
//每隔一定的时间,对长连接进行一次心跳检测
|
||||
mHandler.postDelayed(this, HEART_BEAT_RATE);
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* 开启重连
|
||||
*/
|
||||
private void reconnectWs() {
|
||||
mHandler.removeCallbacks(heartBeatRunnable);
|
||||
new Thread() {
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
Log.i("JWebSocketClientService", "开启重连");
|
||||
client.reconnectBlocking();
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}.start();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user