VinsonGuo / ReconnectWebSocketWrapper

This is a wrapper class based on OkHttp's WebSocket implementation with automatic reconnection. It retains the WebSocket interface, while adding features such as getting connection status and setting status listener.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ReconnectWebSocketWrapper

Introduction

This is a wrapper class based on OkHttp's WebSocket implementation with automatic reconnection. It retains the WebSocket interface, while adding features such as getting connection status and setting connection status listener. It is easy to use and the following is how it is used.

If you don't need to be concerned about reconnection status, you can use it like this

    WebSocket ws = ReconnectWebSocketWrapper(okHttpClient, request, webSocketListener);

If you need the status, you can use it like this

    ReconnectWebSocketWrapper ws = new ReconnectWebSocketWrapper(okHttpClient, request, webSocketListener);
    
    ws.setOnConnectStatusChangeListener(status -> {
        // your code
        return null;
    });
    val ws = ReconnectWebSocketWrapper(okHttpClient, request, webSocketListener)
    webSocketWrapper.onConnectStatusChangeListener = {
        // your code
    }

other api:

    // set reconnection configuration
    webSocketWrapper.config = Config()

    // if reconnection url differ with initial url, you can rebuild a request from this listener
    webSocketWrapper.onPreReconnectListener = { request -> 
        // to build your request
        request
    }

About

This is a wrapper class based on OkHttp's WebSocket implementation with automatic reconnection. It retains the WebSocket interface, while adding features such as getting connection status and setting status listener.

License:Apache License 2.0


Languages

Language:Kotlin 77.6%Language:Java 22.4%