Skip to content

WebSocket channel management for connect/disconnect/reconnect, for both Android and pure Java.

License

Notifications You must be signed in to change notification settings

Gnepux/WebSocketGo

Repository files navigation

Introduction

中文版

[Background] (https://juejin.im/post/5eff3c776fb9a07e662344ff)

WsGo is a java library, it can be used to manage the WebSocket channel.

  1. It helps you to connect, disconnect, auto reconnect, change ping interval.
  2. Support OkHttp、Java WebSocket and any other custom WebSocket library.
  3. Both Android and pure Java platform are supported.
  4. Thread safe.

Download

Gradle

implementation 'com.gnepux:wsgo:1.0.2'
// use okhttp
implementation 'com.gnepux:wsgo-okwebsocket:1.0.1'
// use java websocket
implementation 'com.gnepux:wsgo-jwebsocket:1.0.1'

Maven

<dependency>
    <groupId>com.gnepux</groupId>
    <artifactId>wsgo</artifactId>
    <version>1.0.2</version>
    <type>pom</type>
</dependency>

<!-- use okhttp -->
<dependency>
    <groupId>com.gnepux</groupId>
    <artifactId>wsgo-okwebsocket</artifactId>
    <version>1.0.1</version>
    <type>pom</type>
</dependency>

<!-- use java websocket -->
<dependency>
    <groupId>com.gnepux</groupId>
    <artifactId>wsgo-jwebsocket</artifactId>
    <version>1.0.1</version>
    <type>pom</type>
</dependency>

Usage

1. Init

WsConfig config = new WsConfig.Builder()
                .debugMode(true)    // true to print log
                .setUrl(pushUrl)    // ws url
                .setHttpHeaders(headerMap)  // http headers
                .setConnectTimeout(10 * 1000L)  // connect timeout
                .setReadTimeout(10 * 1000L)     // read timeout
                .setWriteTimeout(10 * 1000L)    // write timeout
                .setPingInterval(10 * 1000L)    // initial ping interval
                .setWebSocket(OkWebSocket.create()) // websocket client
                .setRetryStrategy(retryStrategy)    // retry count and delay time strategy
                .setEventListener(eventListener)    // event listener
                .build();

WsGo.init(config);

2. Go

// connect
WsGo.getInstance().connect();

// send text
WsGo.getInstance().send("hello from WsGo");

// disconnect
WsGo.getInstance().disconnect(1000, "close");
WsGo.getInstance().disconnectNormal("close");

// change the ping interval
WsGo.getInstance().changePingInterval(10, TimeUnit.SECONDS);

// destory WsGo instance
WsGo.getInstance().destroyInstance();

3. More about WsConfig

3.1 setWebSocket(WebSocket socket)

WsGo has already support OkHttp and Java WebSocket.

  • for OkHttp (wsgo-okwebsocket)
setWebSocket(OkWebSocket.create());
  • for Java WebSocket (wsgo-jwebsocket)
setWebSocket(JWebSocket.create());

If you want to use any other WebSocket client. Implementation the WebSocket interface and pass the result to ChannelCallback, then WsGo will help you manage the channel.

public interface WebSocket {
    void connect(WsConfig config, ChannelCallback callback);

    void reconnect(WsConfig config, ChannelCallback callback);

    boolean disconnect(int code, String reason);

    void changePingInterval(long interval, TimeUnit unit);

    boolean send(String msg);
}

3.2 setRetryStrategy(RetryStrategy retryStrategy)

WsGo will auto reconnect if the channel disconnect abnormally. The RetryStrategy means the relationship of retry count and retry delay time.

WsGo has a DefaultRetryStrategy inner, if you want to control it by yourself, you can implementation the RetryStrategy interface.

public interface RetryStrategy {

    /**
     * The relationship of retry count and delay time,
     * WsGo will call the method for every reconnect.
     *
     * @param retryCount The retry time that WsGo has retied.
     * @return The delay time in milliseconds.
     */
    long onRetry(long retryCount);

}

3.3 setEventListener(EventListener eventListener)

Add an EventListener of WsGo, the callback runs in a different thread to the calling thread. You need to switch thread if needed.

public interface EventListener {

    void onConnect();

    void onDisConnect(Throwable throwable);

    void onClose(int code, String reason);

    void onMessage(String text);

    void onReconnect(long retryCount, long delayMillSec);

    void onSend(String text, boolean success);

}

4. Data Flow

dataflow

5. 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.

About

WebSocket channel management for connect/disconnect/reconnect, for both Android and pure Java.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages