GithubHelp home page GithubHelp logo

feivur / rxjava-websocket-client Goto Github PK

View Code? Open in Web Editor NEW

This project forked from jdekim43/rxjava-websocket-client

0.0 0.0 0.0 281 KB

RxJava 2 WebSocket Client for Java

License: MIT License

Java 100.00%

rxjava-websocket-client's Introduction

rxjava-websocket-client

Download

Features

  • Usable any (real connection) Client : OkHttp, etc...
         OkHttp is implemented
  • Acceptable any message type based on String : JSON, XML, TEXT
         Implementation of InboundParser and OutboundSerializer is required.
  • Fabricable 'Observable' which makes 'received message' events you need by using channel and filters.

Dependency

  • RxJava2
  • OkHttp (Optional)

Install

compile "kr.jadekim.rxjava:rxjava-websocket-client:$version"

//or

implementation "kr.jadekim.rxjava:rxjava-websocket-client:$version"

Define client interface

Use builder class

public interface TestClient {
    
    @Channel
    Flowable<String> subscribeAll();
    
    @Channel(value="channel1", filter=RemainderFilter.class)
    Observable<ResponseModel> subscribeChannel1(@Name("dividerValue") int dividerValue,
                                                @Name("remainderValue") int remainderValue);
    
    @Channel(
        value="channel2",
        filter=RemainderFilter.class,
        onStart="requestNumber2(interval, start)",
        onStop="stopNumber2(interval, start)"
    )
    @Params(
        @Param(name="interval", value="1")
    )
    Flowable<ResponseModel> subscribeChannel2(@Name("start") int start,
                                              @Name("dividerValue") int dividerValue,
                                              @Name("remainderValue") int remainderValue);
    
    @Message
    Completable sendMessage(String message);
    
    @Message("requestNumber")
    Completable requestNumber(@Name("interval") int interval);
    
    @Message("requestNumber")
    Completable requestNumber2(@Name("interval") int interval,
                               @Name("startNumber") int startNumber);
    
    @Message("stopNumber")
    Completable stopNumber2(@Name("interval") int interval,
                            @Name("startNumber") int startNumber);
    
    // If method name is 'disconnect', it will be working 'disconnect' action
    void disconnect();
}
JWebSocket webSocketClientFactory = new JWebSocket.Builder().build();
TestClient client = webSocketClientFactory.create(TestClient.class, "ws://localhost/ws");
client.subscribeAll().subscribe(msg -> System.out.println(msg));

Use annotation

@WebSocketClient(
    url = "wss://echo.websocket.org"
)
public interface TestClient {...}

TestClient client = JWebSocket.create(TestClient.class);

Customize

Customize ConnectionFactory (Customize real connection Client)

public class OkHttpConnectionFactory implements ConnectionFactory {
    ...
    public OkHttpConnectionFactory(OkHttpClient okHttpClient) {
        ...
    }
    
    @Override
    public Connection connect(String url, boolean isErrorPropagation, WebSocketEventListener listener) {
        ...
    }
}
OkHttpClient httpClient = new OkHttpClient();
ConnectionFactory connectionFactory = new OkHttpConnectionFactory(httpClient);

Customize Inbound Message Parser

public abstract class GsonInboundParser implements InboundParser<JsonElement> {
    ...
    public GsonInboundParser(Gson gson) {
        ...
    }
    
    public abstract String getChannelName(JsonElement data);
    
    @Override
    public Inbound<JsonElement> parse(String message) {
        ...
    }
    
    @Override
    public <Model> Model mapping(JsonElement object, Type modelType) {
        ...
    }
}
Gson gson = new Gson();

InboundParser parser = new GsonInboundParser(gson) {

    @Override
    public String getChannelName(JsonElement data) {
        return data.getAsJsonObject().get("channel").getAsString(); 
    }
};

Customize Outbound Message Serializer

public class GsonOutboundSerializer implements OutboundSerializer {
    ...
    public GsonOutboundSerializer(Gson gson) {
        ...
    }

    @Override
    public String serialize(String messageType, Map<String, Object> parameterMap) {
        ...
    }
}
Gson gson = new Gson();
OutboundSerializer serializer = new GsonOutboundSerializer(gson);

Configuration

Use builder class

JWebSocket webSocketClientFactory = new JWebSocket.Builder()
                .connectionFactory(connectionFactory)
                .parser(parser)
                .serializer(serializer)
                .lazy(false)
                .errorPropagation(true)
                .build();
                
TestClient client = webSocketClientFactory.create(TestClient.class, "ws://localhost/ws");

Use annotation

@WebSocketClient(
    url = "wss://echo.websocket.org",
    connectionFactory = OkHttpConnectionFactory.class,
    parser = DefaultInboundParser.class,
    serializer = DefaultOutboundSerializer.class,
    listener = SimpleWebSocketEventListener.class,
    isLazy = false,
    isErrorPropagation = true
)
public interface TestClient {...}

TestClient client = JWebSocket.create(TestClient.class);

Lazy (default : false)

When this option is enabled, the 'ConnectionFactory' specified by the user is wrapped in 'LazyConnectionFactory'. 'LazyConnection' created by 'LazyConnectionFactory' automatically terminates the connection when there is no 'Observable' subscribing to the connection. And if the 'sendMessage' method is called, or if there is one or more subscription 'Observable', it automatically creates a connection.

Error propagation (default : true)

This option allows you to set whether or not to notify 'Observable' of errors that occur on the connection. 'Observable' in 'RxJava' will automatically terminate the stream when 'onError' is called. If you want to restore the connection and re-use 'Observable', disable this option.

rxjava-websocket-client's People

Contributors

jdekim43 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.