All Projects → dhhAndroid → Rxwebsocket

dhhAndroid / Rxwebsocket

An auto reconnection-webSocket build with okhttp and rxJava

Programming Languages

java
68154 projects - #9 most used programming language

Projects that are alternatives of or similar to Rxwebsocket

Javawebsocketclient
RxJava WebSocket library for Java and Android
Stars: ✭ 188 (-72.27%)
Mutual labels:  rxjava, websocket, websockets, okhttp
Viabus Architecture
让 Android 开发可以像流水线一样高效的,职责分离架构 ⚡ 不同于 MVP 的配置解耦,也不能和 似是而非 的 MVVM - Clean 同日而语。VIABUS 是世界范围内首个明确提出,通过职责分离,来真正实现 UI 和 业务并行开发的 Android 项目级开发架构和设计模式理念。
Stars: ✭ 485 (-28.47%)
Mutual labels:  rxjava, okhttp
Microwebsrv
A micro HTTP Web server that supports WebSockets, html/python language templating and routing handlers, for MicroPython (used on Pycom modules & ESP32)
Stars: ✭ 420 (-38.05%)
Mutual labels:  websocket, websockets
Retrofitclient
基于Retrofit2.x和Rxjava2.x封装的请求工具类(内含Retrofit2.x & rxjava1.x)
Stars: ✭ 532 (-21.53%)
Mutual labels:  rxjava, okhttp
Wssip
Application for capturing, modifying and sending custom WebSocket data from client to server and vice versa.
Stars: ✭ 373 (-44.99%)
Mutual labels:  websocket, websockets
Java Slack Sdk
Slack Developer Kit (including Bolt for Java) for any JVM language
Stars: ✭ 393 (-42.04%)
Mutual labels:  websocket, websockets
Ratchet
Asynchronous WebSocket server
Stars: ✭ 5,596 (+725.37%)
Mutual labels:  websocket, websockets
Websockets
Library for building WebSocket servers and clients in Python
Stars: ✭ 3,724 (+449.26%)
Mutual labels:  websocket, websockets
Fast Android Networking
🚀 A Complete Fast Android Networking Library that also supports HTTP/2 🚀
Stars: ✭ 5,346 (+688.5%)
Mutual labels:  rxjava, okhttp
Aosf
AOSF:全称为Android Open Source Framework,即Android优秀开源框架汇总。包含:网络请求okhttp,图片下载glide,数据库greenDAO,链式框架RxJava,组件路由ARouter,消息传递通信EventBus,热更新Tinker,插件化框架Replugin,文件下载FileDownloaer,图片选择PhotoPicker,图片滤镜/毛玻璃等特效处理,GIF图片展示控件,图片九宫格控件NineGridView,对话框Dialog,导航指示器ViewpagerIndicator,进度条ProgressWheel,下拉刷新SmartRefreshLayout,key-value高效数据存储MMKV等,应有尽有。
Stars: ✭ 601 (-11.36%)
Mutual labels:  rxjava, okhttp
Lhttp
go websocket, a better way to buid your IM server
Stars: ✭ 603 (-11.06%)
Mutual labels:  websocket, websockets
Net
Android上强大的网络请求
Stars: ✭ 344 (-49.26%)
Mutual labels:  rxjava, okhttp
Ttyd
Share your terminal over the web
Stars: ✭ 4,030 (+494.4%)
Mutual labels:  websocket, websockets
Tokio Tungstenite
Tokio binding for Tungstenite, the Lightweight stream-based WebSocket implementation
Stars: ✭ 392 (-42.18%)
Mutual labels:  websocket, websockets
Java Spring Cloud
Distributed tracing for Spring Boot, Cloud and other Spring projects
Stars: ✭ 326 (-51.92%)
Mutual labels:  rxjava, websocket
Getty
a netty like asynchronous network I/O library based on tcp/udp/websocket; a bidirectional RPC framework based on JSON/Protobuf; a microservice framework based on zookeeper/etcd
Stars: ✭ 532 (-21.53%)
Mutual labels:  websocket, websockets
Cryptofeed
Cryptocurrency Exchange Websocket Data Feed Handler
Stars: ✭ 643 (-5.16%)
Mutual labels:  websocket, websockets
Websocket Client
🔧 .NET/C# websocket client library
Stars: ✭ 297 (-56.19%)
Mutual labels:  websocket, websockets
Python Slack Sdk
Slack Developer Kit for Python
Stars: ✭ 3,307 (+387.76%)
Mutual labels:  websocket, websockets
Androidrapidlibrary
Android 快速开发库,主要想实现一条属于自己的开发框架。包括网络访问,数据,UI等等
Stars: ✭ 577 (-14.9%)
Mutual labels:  rxjava, okhttp

RxWebSocket

招聘:我司正在招聘Android架构师职位,如有兴趣,请点击[email protected] 邮箱,期待你的到来!

Download API License

RxWebSocket是一个基于okhttp和RxJava(RxJava1和RxJava2都有)封装的WebSocket客户端,此库的核心特点是 除了手动关闭WebSocket(就是RxJava取消订阅),WebSocket在异常关闭的时候(onFailure,发生异常,如WebSocketException等等),会自动重连,永不断连.其次,对WebSocket做的缓存处理,同一个URL,共享一个WebSocket.

原理解析: 戳我戳我戳我

RxJava2版本点我(RxJava2和RxJava1调用方式一样)

查看changeLog

效果图

断网重连测试

断网重连测试

how to use

添加依赖:

在项目module下gradle加入:

    implementation 'com.dhh:websocket:2.1.2'

init

        //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里设置心跳间隔:

        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是一个没有抽象方法的抽象类,根据业务需求,重写你想使用的回调

        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

        /**
         *
         *如果你想将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) {

                    }
                });

发送消息

        //用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的注销方式,就可以取消订阅.

    Subscription subscription = RxWebSocket.get("ws://sdfs").subscribe();
    //注销
    if(subscription!=null&&!subscription.isUnsubscribed()) {
        subscription.unsubscribe();
    }

更优雅的注销处理方式,请看我的另一个项目: RxLife,优雅地处理RxJava注销问题,和Activity生命周期绑定.

如果本库对你有帮助,谢谢您的star!

RxJava交流群

点击加群

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.
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].