Awesome Open Source
Awesome Open Source

RxWebSocket

Android Android [email protected]

Download API License

RxWebSocketokhttpRxJava(RxJava1RxJava2)WebSocket, WebSocket(RxJava),WebSocket(onFailure,,WebSocketException),,.,WebSocket,URL,WebSocket.

:

RxJava2(RxJava2RxJava1)

changeLog

how to use

:

modulegradle:

    implementation 'com.dhh:websocket:2.1.2'

init


        //init config RxWebSocketapplication
        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,okhttpSSL,okhttpConfig

okhttpClientConfig


        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 WebSocketSubscriberWebSocketSubscriber


        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:");
                    }
                });

Stringtext, WebSocketSubscriber2

        /**
         *
         *Stringtext{@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.

Get A Weekly Email With Trending Projects For These Topics
No Spam. Unsubscribe easily at any time.
Java (706,068
Android (40,484
Websocket (5,264
Websocket (5,264
Rxjava (1,335
Okhttp (363
Related Projects