介绍之类的就不多讲了,懒得讲也未必有别人整理的清晰,直接上代码文章来源:https://www.toymoban.com/news/detail-607859.html
//okhttp-websocket
implementation 'com.squareup.okhttp3:mockwebserver:3.8.1'
使用:文章来源地址https://www.toymoban.com/news/detail-607859.html
private WebSocket mWebSocket;
private OkHttpClient mClient;
private boolean isReceivePong;
private Animation operatingAnim;
/**
* WS初始化
* */
public void WSinit() {
//连接前等待动画
operatingAnim = AnimationUtils.loadAnimation(this, R.anim.waiting);
LinearInterpolator lin = new LinearInterpolator();
operatingAnim.setInterpolator(lin);
mClient = new OkHttpClient.Builder()
.pingInterval(10, TimeUnit.SECONDS)
.build();
WSConnect();
}
/**
* WS连接
* */
public void WSConnect() {
//关闭心跳发送,避免心跳重复
heartHandler.removeCallbacksAndMessages(null);
//开启前等待动画
if (operatingAnim != null) {
imgWebsocketStatus.setImageResource(R.drawable.waiting);
imgWebsocketStatus.startAnimation(operatingAnim);
}
Request request = new Request.Builder()
.url("ws://192.168.30.100:8044")
.build();
mWebSocket = mClient.newWebSocket(request, new WsListener());
}
/**
* 回调
* */
class WsListener extends WebSocketListener {
@Override
public void onClosed(WebSocket webSocket, int code, String reason) {
super.onClosed(webSocket, code, reason);
//连接关闭...
LogUtils.tag(TAG).e("WS连接关闭!"+code+reason);
}
@Override
public void onClosing(WebSocket webSocket, int code, String reason) {
super.onClosing(webSocket, code, reason);
//客户端主动关闭时回调
LogUtils.tag(TAG).e("连接中!" + code);
}
@Override
public void onFailure(WebSocket webSocket, Throwable t, @Nullable Response response) {
super.onFailure(webSocket, t, response);
// 出错了
Message message = new Message();
message.what = 401;
mHandler.sendMessage(message);
LogUtils.tag(TAG).e("WS连接错误!错误信息:" + t.getMessage());
}
@Override
public void onMessage(WebSocket webSocket, String text) {
super.onMessage(webSocket, text);
// 收到服务端发送来的 String 类型消息
if (text.equals("Heartbeat")){
isReceivePong = true;
Log.e(TAG,"WS收到心跳,Pong="+isReceivePong);
}else {
LogUtils.tag(TAG).e("WS收到消息:" + text);
Message message = new Message();
message.what = 0;
message.obj = text;
mHandler.sendMessage(message);
}
}
@Override
public void onMessage(WebSocket webSocket, ByteString bytes) {
super.onMessage(webSocket, bytes);
//收到消息...(一般很少这种消息)
}
@Override
public void onOpen(WebSocket webSocket, Response response) {
super.onOpen(webSocket, response);
//连接成功...
//主动发送心跳包
isReceivePong = true;
//开启心跳
heartHandler.sendEmptyMessage(10);
//连接成功后关闭等待动画
Message message = new Message();
message.what = 400;
mHandler.sendMessage(message);
mWebSocket = webSocket;
//测试发消息
send("Heartbeat");
}
}
/**
* WS发送心跳包
* */
Handler heartHandler = new Handler(Looper.getMainLooper(), new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if (msg.what != 10) return false;
Log.e(TAG,"WS轮询时,Pong="+isReceivePong);
if (isReceivePong) {
send("Heartbeat");
//isReceivePong置false,等待服务器返回心跳时置ture,如果服务器没有返回则表示连接断开
isReceivePong = false;
heartHandler.sendEmptyMessageDelayed(10, 10000);
} else {
Log.e(TAG,"关闭重连");
//没有收到pong命令,进行重连
WSConnect();
}
return false;
}
});
/**
* WS发送消息
*
* @param message
*/
public void send(final String message) {
if (mWebSocket != null) {
if (!message.equals("Heartbeat")){
LogUtils.tag(TAG).e("WS发送消息:" + message);
}
mWebSocket.send(message);
}
}
/**
* WS主动断开连接
*
* @param code
* @param reason
*/
public void disconnect(int code, String reason) {
if (mWebSocket != null)
mWebSocket.close(code, reason);
}
到了这里,关于Android中okhttp的websocket的详细使用方法(加断线重连)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!