里面加入了断线重连、心跳机制、退出释放资源,废话不多直接开干 1.添加依赖
implementation "org.java-websocket:Java-WebSocket:1.5.1"
2、添加使用类
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;
import javax.net.ssl.SSLParameters;
public class JWebSocketClient extends WebSocketClient {
@Override
protected void onSetSSLParameters(SSLParameters sslParameters) {
}
public JWebSocketClient(URI serverUri) {
super(serverUri, new Draft_6455());
}
@Override
public void onOpen(ServerHandshake handshakedata) {
Log.e("JWebSocketClient", "onOpen()");
}
@Override
public void onMessage(String message) {
Log.e("JWebSocketClient", "onMessage()");
}
@Override
public void onClose(int code, String reason, boolean remote) {
Log.e("JWebSocketClient", "onClose()" + reason + "-****-" + remote);
}
@Override
public void onError(Exception ex) {
Log.e("JWebSocketClient", "onError()" + ex);
}
}
3、在代码中的使用
public class BloodSugarActivity extends BaseActivity {
String TAG="BloodSugarActivity";
public JWebSocketClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
setContentView(R.layout.activity_blood_sugar);
}
@Override
protected void onResume() {
super.onResume();
mHandler.postDelayed(heartBeatRunnable, HEART_BEAT_RATE);
if (client == null) {
Log.e(TAG, "``````````````````````onResume");
initWebSocket();
} else if (!client.isOpen()) {
reconnectWs();
}
}
@Override
protected void onStop() {
super.onStop();
Log.e(TAG, "``````````````````````````````onStop");
}
@Override
protected void onDestroy() {
super.onDestroy();
Log.e(TAG, "`````````````````````````onDestroy");
closeConnect();
}
public void initWebSocket() {
Log.e(TAG, "websocket的地址是:we://" + SpUtils.decodeString("websocketurl"));
URI uri = URI.create("ws://" + SpUtils.decodeString("websocketurl"));
client = new JWebSocketClient(uri) {
@Override
public void onMessage(String message) {
super.onMessage(message);
if (!message.equals("Heartbeat")){
LogUtils(TAG, "websocket收到消息:" + message);
}
}
@Override
public void onOpen(ServerHandshake handshakedata) {
super.onOpen(handshakedata);
LogUtils(TAG, "websocket连接成功");
}
@Override
public void onError(Exception ex) {
super.onError(ex);
LogUtils(TAG, "websocket连接错误:" + ex);
}
@Override
public void onClose(int code, String reason, boolean remote) {
super.onClose(code, reason, remote);
if (code!=1000) {
reconnectWs();
}
LogUtils(TAG, "websocket断开连接:·code:" + code + "·reason:" + reason + "·remote:" + remote);
}
};
client.setConnectionLostTimeout(110 * 1000);
new Thread() {
@Override
public void run() {
try {
client.connectBlocking();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
public void sendMsg(String msg) {
if (null != client) {
Log.e("", "^_^Websocket发送的消息:-----------------------------------^_^" + msg);
if (client.isOpen()) {
client.send(msg);
}
}
}
private void reconnectWs() {
mHandler.removeCallbacks(heartBeatRunnable);
new Thread() {
@Override
public void run() {
try {
Log.e("开启重连", "");
client.reconnectBlocking();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
}
private void closeConnect() {
try {
if (null != client) {
client.close();
}
if (mHandler != null) {
mHandler.removeCallbacksAndMessages(null);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
client = null;
}
}
private static final long HEART_BEAT_RATE = 10 * 1000;
private Handler mHandler = new Handler();
private Runnable heartBeatRunnable = new Runnable() {
@Override
public void run() {
if (client != null) {
if (client.isClosed()) {
Log.e("心跳包检测websocket连接状态1", client.isOpen() + "/" + SpUtils.decodeString("websocketurl"));
reconnectWs();
} else {
Log.e("心跳包检测websocket连接状态2", client.isOpen() + "/" + SpUtils.decodeString("websocketurl"));
sendMsg("Heartbeat");
}
} else {
Log.e("心跳包检测websocket连接状态重新连接", "");
client = null;
initSocketClient();
}
mHandler.postDelayed(this, HEART_BEAT_RATE);
}
};
}
后续把配合service后台一起使用的方法写出来
|