目前Android WebSocket 框架 主要包括:
- SocketIO
- Java-WebSocket
- OkHttp WebSocket
一開始我首選的是采用SocketIO方案担锤,因?yàn)榭紤]該方案封裝接口好,提供異步回調(diào)機(jī)制,但和后端同事溝通發(fā)現(xiàn)目前客戶端的SocketIO不支持ws wss協(xié)議, 所以無奈只能放棄。
接著考慮采用Java-WebSocket方案重荠,該方案是websocket的java完整實(shí)現(xiàn)弊知,目前github6.5K星,于是考慮導(dǎo)入妻坝,但是在實(shí)測時發(fā)現(xiàn)調(diào)用connect盲赊,reConnect峦剔,如果導(dǎo)致線程異常報錯,網(wǎng)上搜索相關(guān)解決方案角钩,并不能有效解決此問題,當(dāng)然也可能是我沒有更深入分析此問題呻澜。
最后考慮采用OkHttp方案递礼,基于OkHttp優(yōu)秀的線程讀寫控制機(jī)制,發(fā)現(xiàn)該方案出奇的穩(wěn)定羹幸。
參考文檔:https://square.github.io/okhttp/4.x/okhttp/okhttp3/-web-socket/
以下是對OkHttp websocket的簡單封裝
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() {
//構(gòu)造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
}