Android開源 - RxWebSocket:基于 OkHttp 和 RxJava 封裝的 WebSocket 客戶端
RxWebSocket是一個基于okhttp和RxJava封裝的WebSocket客戶端,此庫的核心特點是 除了手動關閉WebSocket(就是RxJava取消訂閱),WebSocket在異常關閉的時候(onFailure,發生異常,如WebSocketException等等),會自動重連,永不斷連.其次,對WebSocket做的緩存處理,同一個URL,共享一個WebSocket.
原理解析: 戳我戳我戳我
效果圖
斷網重連測試
how to use
添加依賴:
本項目依賴 okhttp和RxJava,RxAndroid開發,所以在module下除了加入本項目依賴,還需加入okhttp和RxJava,RxAndroid依賴:
//本項目
compile 'com.dhh:websocket:1.3.0'
//okhttp,RxJava,RxAndroid
compile 'com.squareup.okhttp3:okhttp:3.9.0'
compile 'io.reactivex:rxjava:1.3.1'
compile 'io.reactivex:rxandroid:1.2.1'
init
//if you want to use your okhttpClient
OkHttpClient yourClient = new OkHttpClient();
RxWebSocketUtil.getInstance().setClient(yourClient);
// show log,default false
RxWebSocketUtil.getInstance().setShowLog(true);
open WebSocket
RxWebSocketUtil.getInstance().getWebSocketInfo(url)
.subscribe(new Action1<WebSocketInfo>() {
@Override
public void call(WebSocketInfo webSocketInfo) {
mWebSocket = webSocketInfo.getWebSocket();
Log.d("MainActivity", webSocketInfo.getString());
Log.d("MainActivity", "webSocketInfo.getByteString():" + webSocketInfo.getByteString());
}
});
mWebSocket.send("hello word");
//get StringMsg
RxWebSocketUtil.getInstance().getWebSocketString(url)
.subscribe(new Action1<String>() {
@Override
public void call(String s) {
}
});
// get ByteString
RxWebSocketUtil.getInstance().getWebSocketByteString(url)
.subscribe(new Action1<ByteString>() {
@Override
public void call(ByteString byteString) {
}
});
//get WebSocket
RxWebSocketUtil.getInstance().getWebSocket(url)
.subscribe(new Action1<WebSocket>() {
@Override
public void call(WebSocket webSocket) {
}
});
//with timeout
RxWebSocketUtil.getInstance().getWebSocketInfo(url, 10, TimeUnit.SECONDS)
.subscribe(new Action1<WebSocketInfo>() {
@Override
public void call(WebSocketInfo webSocketInfo) {
}
});
// Rxbinding
RxView.clicks(centect)
.flatMap(new Func1<Void, Observable<String>>() {
@Override
public Observable<String> call(Void aVoid) {
return RxWebSocketUtil.getInstance().getWebSocketString(url);
}
})
.subscribe(new Action1<String>() {
@Override
public void call(String s) {
//the s !=null
Log.d("MainActivity", s);
textview.setText(Html.fromHtml(s));
}
});
發送消息
//用WebSocket的引用直接發
mWebSocket.send("hello word");
//url 對應的WebSocket已經打開可以這樣send,否則報錯
RxWebSocketUtil.getInstance().send(url, "hello");
RxWebSocketUtil.getInstance().send(url, ByteString.EMPTY);
//異步發送,若WebSocket已經打開,直接發送,若沒有打開,打開一個WebSocket發送完數據,直接關閉.
RxWebSocketUtil.getInstance().asyncSend(url, "hello");
RxWebSocketUtil.getInstance().asyncSend(url, ByteString.EMPTY);
注銷
RxJava的注銷方式,就可以取消訂閱. 項目里的demo里,簡單實現了一個Lifecycle.僅供參考.
Subscription subscription = RxWebSocketUtil.getInstance().getWebSocketString("ws://sdfs").subscribe();
//注銷
if(subscription!=null&&!subscription.isUnsubscribed()) {
subscription.unsubscribe();
}
更優雅的注銷處理方式,請看我的另一個項目: RxLifecycle ,優雅地處理RxJava注銷問題,和Activity生命周期綁定.
如果本庫對你有幫助,謝謝您的star!
License
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
本文由用戶 lihongyang033 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!