1 Star 1 Fork 0

奇缘/RxWebSocket

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0
# RxWebSocket # ## 此项目不再维护,请慎重使用,可查看源码学习! [![](https://img.shields.io/badge/platform-android-brightgreen.svg)](https://developer.android.com/index.html) [ ![Download](https://api.bintray.com/packages/dhhandroid/maven/rxwebsocket/images/download.svg) ](https://bintray.com/dhhandroid/maven/rxwebsocket/_latestVersion) [ ![API](https://img.shields.io/badge/API-11%2B-blue.svg?style=flat-square) ](https://developer.android.com/about/versions/android-3.0.html) [ ![License](http://img.shields.io/badge/License-Apache%202.0-blue.svg?style=flat-square) ](http://www.apache.org/licenses/LICENSE-2.0) ## RxWebSocket是一个基于okhttp和RxJava(RxJava1和RxJava2都有)封装的WebSocket客户端,此库的核心特点是 除了手动关闭WebSocket(就是RxJava取消订阅),WebSocket在异常关闭的时候(onFailure,发生异常,如WebSocketException等等),会自动重连,永不断连.其次,对WebSocket做的缓存处理,同一个URL,共享一个WebSocket. ## 原理解析: [戳我戳我戳我](http://blog.csdn.net/huiAndroid/article/details/78071703) ## [RxJava2版本点我(RxJava2和RxJava1调用方式一样)](https://github.com/dhhAndroid/RxWebSocket/tree/2.x) ### [查看changeLog](https://github.com/dhhAndroid/RxWebSocket/blob/1.x/ChangeLog.md) ## 效果图 ## ![](image/WebSocket.gif) ### 断网重连测试 ![断网重连测试](image/recontection.gif) ## how to use ## ### 添加依赖: ### #### 在项目module下gradle加入: ```gradle implementation 'com.dhh:websocket:2.1.2' ``` ### init ```java //init config 在使用RxWebSocket之前设置即可,推荐在application里初始化 Config config = new Config.Builder() .setShowLog(true) //show log .setClient(yourClient) //if you want to set your okhttpClient .setShowLog(true, "your logTag") .setReconnectInterval(2, TimeUnit.SECONDS) //set reconnect interval .setSSLSocketFactory(yourSSlSocketFactory, yourX509TrustManager) // wss support .build(); RxWebSocket.setConfig(config); ``` ### WSS support,其实就是设置okhttp的SSL,请参照okhttp的设置,请参照上面Config配置 ### 心跳检测:需要设置自己的okhttpClient,在上面的Config里设置心跳间隔: ```java Config config = new Config.Builder() .setClient(new OkHttpClient.Builder() .pingInterval(3, TimeUnit.SECONDS) // 设置心跳间隔,这个是3秒检测一次 .build()) //if you want to set your okhttpClient .build(); ``` ### open WebSocket:和RxJava调用一样,回调请使用项目里提供的 **WebSocketSubscriber**,WebSocketSubscriber是一个没有抽象方法的抽象类,根据业务需求,重写你想使用的回调 ```java RxWebSocket.get("url") .subscribe(new WebSocketSubscriber() { @Override protected void onMessage(@NonNull String text) { } }); RxWebSocket.get("your url") //RxLifecycle : https://github.com/dhhAndroid/RxLifecycle .compose(RxLifecycle.with(this).<WebSocketInfo>bindToLifecycle()) .subscribe(new WebSocketSubscriber() { @Override public void onOpen(@NonNull WebSocket webSocket) { Log.d("MainActivity", "onOpen1:"); } @Override public void onMessage(@NonNull String text) { Log.d("MainActivity", "返回数据:" + text); } @Override public void onMessage(@NonNull ByteString byteString) { } @Override protected void onReconnect() { Log.d("MainActivity", "重连:"); } @Override protected void onClose() { Log.d("MainActivity", "onClose:"); } }); ``` #### 如果你想将String类型的text解析成具体的实体类,请使用 **WebSocketSubscriber2** ```java /** * *如果你想将String类型的text解析成具体的实体类,比如{@link List<String>}, * 请使用 {@link WebSocketSubscriber2},仅需要将泛型传入即可 */ RxWebSocket.get("your url") .compose(RxLifecycle.with(this).<WebSocketInfo>bindToLifecycle()) .subscribe(new WebSocketSubscriber2<List<String>>() { @Override protected void onMessage(List<String> strings) { } }); ``` ### 发送消息 ### ```java //用WebSocket的引用直接发 mWebSocket.send("hello word"); //url 对应的WebSocket 必须打开,否则报错 RxWebSocket.send(url, "hello"); RxWebSocket.send(url, ByteString.EMPTY); //异步发送,若WebSocket已经打开,直接发送,若没有打开,打开一个WebSocket发送完数据,直接关闭. RxWebSocket.asyncSend(url, "hello"); RxWebSocket.asyncSend(url, ByteString.EMPTY); ``` ### 注销 ### RxJava的注销方式,就可以取消订阅. ```java Subscription subscription = RxWebSocket.get("ws://sdfs").subscribe(); //注销 if(subscription!=null&&!subscription.isUnsubscribed()) { subscription.unsubscribe(); } ``` ## 更优雅的注销处理方式,请看我的另一个项目: [RxLife](https://github.com/dhhAndroid/RxLife),优雅地处理RxJava注销问题,和Activity生命周期绑定. 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.

简介

暂无描述 展开 收起
Java
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/strange_luck/RxWebSocket.git
git@gitee.com:strange_luck/RxWebSocket.git
strange_luck
RxWebSocket
RxWebSocket
1.x

搜索帮助