日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

android websocket封装,Android WebSocket 方案选型OkHttp

發布時間:2023/12/4 Android 77 豆豆
生活随笔 收集整理的這篇文章主要介紹了 android websocket封装,Android WebSocket 方案选型OkHttp 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

目前Android WebSocket 框架 主要包括:

SocketIO

Java-WebSocket

OkHttp WebSocket

一開始我首選的是采用SocketIO方案,因為考慮該方案封裝接口好,提供異步回調機制,但和后端同事溝通發現目前客戶端的SocketIO不支持ws wss協議, 所以無奈只能放棄。

接著考慮采用Java-WebSocket方案,該方案是websocket的java完整實現,目前github6.5K星,于是考慮導入,但是在實測時發現調用connect,reConnect,如果導致線程異常報錯,網上搜索相關解決方案,并不能有效解決此問題,當然也可能是我沒有更深入分析此問題。

最后考慮采用OkHttp方案,基于OkHttp優秀的線程讀寫控制機制,發現該方案出奇的穩定。

public class WebSocketHandler extends WebSocketListener {

private static final String TAG = "WebSocketHandler ";

private String wsUrl;

private WebSocket webSocket;

private ConnectStatus status;

private OkHttpClient client = new OkHttpClient.Builder()

.build();

private WebSocketHandler(String wsUrl) {

this.wsUrl = wsUrl;

}

private static WebSocketHandler INST;

public static WebSocketHandler getInstance(String url) {

if (INST == null) {

synchronized (WebSocketHandler.class) {

INST = new WebSocketHandler(url);

}

}

return INST;

}

public ConnectStatus getStatus() {

return status;

}

public void connect() {

//構造request對象

Request request = new Request.Builder()

.url(wsUrl)

.build();

webSocket = client.newWebSocket(request, this);

status = ConnectStatus.Connecting;

}

public void reConnect() {

if (webSocket != null) {

webSocket = client.newWebSocket(webSocket.request(), this);

}

}

public void send(String text) {

if (webSocket != null) {

log("send: " + text);

webSocket.send(text);

}

}

public void cancel() {

if (webSocket != null) {

webSocket.cancel();

}

}

public void close() {

if (webSocket != null) {

webSocket.close(1000, null);

}

}

@Override

public void onOpen(WebSocket webSocket, Response response) {

super.onOpen(webSocket, response);

log("onOpen");

this.status = ConnectStatus.Open;

if (mSocketIOCallBack != null) {

mSocketIOCallBack.onOpen();

}

}

@Override

public void onMessage(WebSocket webSocket, String text) {

super.onMessage(webSocket, text);

log("onMessage: " + text);

if (mSocketIOCallBack != null) {

mSocketIOCallBack.onMessage(text);

}

}

@Override

public void onMessage(WebSocket webSocket, ByteString bytes) {

super.onMessage(webSocket, bytes);

}

@Override

public void onClosing(WebSocket webSocket, int code, String reason) {

super.onClosing(webSocket, code, reason);

this.status = ConnectStatus.Closing;

log("onClosing");

}

@Override

public void onClosed(WebSocket webSocket, int code, String reason) {

super.onClosed(webSocket, code, reason);

log("onClosed");

this.status = ConnectStatus.Closed;

if (mSocketIOCallBack != null) {

mSocketIOCallBack.onClose();

}

}

@Override

public void onFailure(WebSocket webSocket, Throwable t, @Nullable Response response) {

super.onFailure(webSocket, t, response);

log("onFailure: " + t.toString());

t.printStackTrace();

this.status = ConnectStatus.Canceled;

if (mSocketIOCallBack != null) {

mSocketIOCallBack.onConnectError(t);

}

}

private WebSocketCallBack mSocketIOCallBack;

public void setSocketIOCallBack(WebSocketCallBack callBack) {

mSocketIOCallBack = callBack;

}

public void removeSocketIOCallBack() {

mSocketIOCallBack = null;

}

}

public enum ConnectStatus {

Connecting, // the initial state of each web socket.

Open, // the web socket has been accepted by the remote peer

Closing, // one of the peers on the web socket has initiated a graceful shutdown

Closed, // the web socket has transmitted all of its messages and has received all messages from the peer

Canceled // the web socket connection failed

}

總結

以上是生活随笔為你收集整理的android websocket封装,Android WebSocket 方案选型OkHttp的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。