目前Android WebSocket 框架 主要包括:
- SocketIO
- Java-WebSocket
- OkHttp WebSocket
一开始我首选的是采用SocketIO方案,因为考虑该方案封装接口好,提供异步回调机制,但和后端同事沟通发现目前客户端的SocketIO不支持ws wss协议, 所以无奈只能放弃。
接着考虑采用Java-WebSocket方案,该方案是websocket的java完整实现,目前github6.5K星,于是考虑导入,但是在实测时发现调用connect,reConnect,如果导致线程异常报错,网上搜索相关解决方案,并不能有效解决此问题,当然也可能是我没有更深入分析此问题。
最后考虑采用OkHttp方案,基于OkHttp优秀的线程读写控制机制,发现该方案出奇的稳定。
参考文档: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() {
//构造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
}