Skip to content

Repository files navigation

JavaWebsocketClient also for Android

JavaWebsocketClient is library is simple library for Websocket connection in rx for java and Android. It is designed to be fast and fault tolerant.

Currently we use okhttp3 for websocket connection because okhttp3 is simple and well tested solution.

Build Status

Release

Presentation of example

Content of the package

  • Example websockets server python twisted server
  • Rx-java websocket client library websockets-rxjava/
  • Rx-java websocket android example websockets-rxjava-example/

Reactive example

How to connect to server:

finalRequestrequest = newRequest.Builder()
.get()
.url("ws://10.10.0.2:8080/ws")
.build();
finalSubscriptionsubscribe = newRxWebSockets(newOkHttpClient(), request)
.webSocketObservable()
.subscribe(newAction1<RxEvent>() {
@Overridepublicvoidcall(RxEventrxEvent) {
System.out.println("Event: " + rxEvent.toString());
}
});
Thread.sleep(10000);
subscribe.unsubscribe();

Send message on connected:

finalSubscriptionsubscription = newRxWebSockets(newWebSocket, request)
.webSocketObservable()
.subscribe(newAction1<RxEvent>() {
@Overridepublicvoidcall(RxEventrxEvent) {
if (rxEventinstanceofRxEventConnected) {
Observable.just("response")
.compose(RxMoreObservables.sendMessage((RxEventConnected) rxEvent))
.subscribe();
}
}
});
Thread.sleep(1000);
subscription.unsubscribe();

For examples look:

Rx-java with json parser

classYourMessage {
publicStringresponse;
publicStringerror;
}
finalRequestrequest = newRequest.Builder()
.get()
.url("ws://10.10.0.2:8080/ws")
.build();
finalRxWebSocketsrxWebSockets = newRxWebSockets(newOkHttpClient(), request)
finalObjectSerializerserializer = newGsonObjectSerializer(newGson(), Message.class)
finalRxObjectWebSocketswebSockets = newRxObjectWebSockets(rxWebSockets), serializer);
finalSubscriptionsubscription = webSockets.webSocketObservable()
.compose(MoreObservables.filterAndMap(RxObjectEventMessage.class))
.compose(RxObjectEventMessage.filterAndMap(YourMessage.class))
.subscribe(newAction1<YourMessage>() {
@Overridepublicvoidcall(YourMessageyourMessage) {
System.out.println("your message: " + yourMessage.response);
}
});
Thread.sleep(1000);
subscription.unsubscribe();

Run example from gradle

To run example first run websocket server, than update url to your host in:

./gradlew :websockets-rxjava-example:installDebug

How to add to your project

to your gradle file:

repositories {
maven { url "https://jitpack.io" }
}
dependencies {
// snapshot version
compile 'com.github.jacek-marchwicki:JavaWebsocketClient:master-SNAPSHOT'// or use specific version
compile 'com.github.jacek-marchwicki:JavaWebsocketClient:<look-on-release-tab>'
}

License

Copyright [2015] [Jacek Marchwicki <jacek.marchwicki@gmail.com>]
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

RxJava WebSocket library for Java and Android

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages