update:2019.04.11

fix:增加在线状态
add:
This commit is contained in:
2020-04-11 18:36:26 +08:00
parent f51ceac69d
commit ecf5aa3d1c
3 changed files with 176 additions and 3 deletions

View 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());
}
}

View File

@@ -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();
}
}