android websocket封装,Android WebSocket 方案选型OkHttp
目前Android WebSocket 框架 主要包括:
SocketIO
Java-WebSocket
OkHttp WebSocket
一開始我首選的是采用SocketIO方案,因為考慮該方案封裝接口好,提供異步回調機制,但和后端同事溝通發(fā)現(xiàn)目前客戶端的SocketIO不支持ws wss協(xié)議, 所以無奈只能放棄。
接著考慮采用Java-WebSocket方案,該方案是websocket的java完整實現(xiàn),目前github6.5K星,于是考慮導入,但是在實測時發(fā)現(xiàn)調用connect,reConnect,如果導致線程異常報錯,網上搜索相關解決方案,并不能有效解決此問題,當然也可能是我沒有更深入分析此問題。
最后考慮采用OkHttp方案,基于OkHttp優(yōu)秀的線程讀寫控制機制,發(fā)現(xiàn)該方案出奇的穩(wěn)定。
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么在不知道WIFI密码的时候通过扫一扫
- 下一篇: android 导航 美国,变美了 An