Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scarlet in Java/Android #12

Closed
subzero10 opened this issue Jun 30, 2018 · 10 comments
Closed

Scarlet in Java/Android #12

subzero10 opened this issue Jun 30, 2018 · 10 comments

Comments

@subzero10
Copy link

Hi, I'm having trouble integrating Scarlet into my Android app written in Java. Can you provide an example please (maybe put it in the Readme section along with the Kotlin example)?

@subzero10
Copy link
Author

28 days and no response. I will just close this and thank you all for your help.

@mccccccmike
Copy link

Kotlin is one hundred percent interoperable with Java ,so you use Scarlet in your Java Project is totally fine.

@subzero10
Copy link
Author

I understand that. But, I could not find a way to provide a WebSocket.Factory and a MessageAdapter.Factory. For Message Adapter, I want to use Gson.

Scarlet scarletInstance = new Scarlet.Builder()
                .webSocketFactory(<PROBLEM_HERE>)
                .addMessageAdapterFactory(<PROBLEM_HERE>)
                .build();

@mccccccmike
Copy link

In build.gradle(Module:app), add these lines of code to support Kotlin in your Java project
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
For WebSocket.Factory,you need to create a kotlin file named for example OkHttpClientProvider.kt,
fun getOkHttpClient(client: OkHttpClient) =
client.newWebSocketFactory("ws://test.asdfd.com:2346/")
and
Scarlet scarlet = new Scarlet.Builder()
.webSocketFactory(OKHttpClientProviderKt.getOkHttpClient(client))
.lifecycle(lifecycle)
.addMessageAdapterFactory(new GsonMessageAdapter.Factory())
.addStreamAdapterFactory(new RxJava2StreamAdapterFactory())
.build();
we are done.

@subzero10
Copy link
Author

Awesome. Thank you very much!

@vivekpanchal
Copy link

Hey, @subzero10 have you implemented scarlet in your application ??
can you please provide the link to your application so that I can refer to it?

@slott
Copy link

slott commented Mar 4, 2019

@vivekpanchal

Try this

		webSocketInterface = new Scarlet.Builder()
				.webSocketFactory(OkHttpClientUtils.newWebSocketFactory(okClient, "ws://echo.websocket.org"))
				.addMessageAdapterFactory(new GsonMessageAdapter.Factory())
				.build().create(VivinoWebSocketInterface.class);;

And then setup the interface like so

public interface VivinoWebSocketInterface {
	@Send
	void subscribe(String message);

	@Receive
	Stream<String> observeTicker();
}

And observe like this

getWebSocketInterface().observeTicker().start(new Stream.Observer<String>() {
			@Override
			public void onNext(String receivedMessage) {
				Log.d(TAG, "onNext");

			}

			@Override
			public void onError(Throwable throwable) {
				Log.d(TAG, "onError");

			}

			@Override
			public void onComplete() {
				Log.d(TAG, "onComplete");

			}
		});

And send message like this

		getWebSocketInterface().subscribe("my message");

@vivekpanchal
Copy link

thanks, @slott for this easy implementation.

@slott
Copy link

slott commented Mar 5, 2019

thanks, @slott for this easy implementation.

I have it up and running with header authentication as well if that is something you need. It seems server errors aren't really relayed so you need to add breakpoints inside the lib to figure out whats going on.

@swdmnd
Copy link

swdmnd commented Apr 29, 2020

@vivekpanchal

Try this

		webSocketInterface = new Scarlet.Builder()
				.webSocketFactory(OkHttpClientUtils.newWebSocketFactory(okClient, "ws://echo.websocket.org"))
				.addMessageAdapterFactory(new GsonMessageAdapter.Factory())
				.build().create(VivinoWebSocketInterface.class);;

And then setup the interface like so

public interface VivinoWebSocketInterface {
	@Send
	void subscribe(String message);

	@Receive
	Stream<String> observeTicker();
}

And observe like this

getWebSocketInterface().observeTicker().start(new Stream.Observer<String>() {
			@Override
			public void onNext(String receivedMessage) {
				Log.d(TAG, "onNext");

			}

			@Override
			public void onError(Throwable throwable) {
				Log.d(TAG, "onError");

			}

			@Override
			public void onComplete() {
				Log.d(TAG, "onComplete");

			}
		});

And send message like this

		getWebSocketInterface().subscribe("my message");

Thank you for the example.
Could you implement this code to subscribe to WebSocket Events?

EDIT: nvm, I did it using this code

        service.observeEvent().subscribe(new Consumer<WebSocket.Event>(){

            @Override
            public void accept(WebSocket.Event event) throws Exception {
                Log.d(TAG, event.getClass().toString());
                if( event.getClass().equals(WebSocket.Event.OnConnectionOpened.class) ){
                    Log.d(TAG,"connection opened.");
                }
            }
        });

Where observeEvent() returns Flowable<WebSocket.Event>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants