I wanted to know if there is any java implementation of Scarlet web socket library.
@EchoBotScope
@Component(modules = [(EchoBotComponent.EchoBotModule::class)], dependencies = [(EchoBotComponent.Dependency::class)])
interface EchoBotComponent {
fun inject(echoBotFragment: EchoBotFragment)
interface Dependency {
fun application(): Application
}
@Component.Builder
interface Builder {
fun dependency(dependency: Dependency): Builder
fun build(): EchoBotComponent
}
@Module
class EchoBotModule {
@Provides
@EchoBotScope
fun provideOkHttpClient(): OkHttpClient = OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BASIC))
.build()
@Provides
@EchoBotScope
fun provideLifecycle(application: Application, loggedInLifecycle: LoggedInLifecycle): Lifecycle =
AndroidLifecycle.ofApplicationForeground(application)
.combineWith(loggedInLifecycle)
@Provides
@EchoBotScope
fun provideEchoService(client: OkHttpClient, lifecycle: Lifecycle): EchoService {
val scarlet = Scarlet.Builder()
.webSocketFactory(client.newWebSocketFactory("wss://demos.kaazing.com/echo"))
.lifecycle(lifecycle)
.addMessageAdapterFactory(BitmapMessageAdapter.Factory())
.addStreamAdapterFactory(RxJava2StreamAdapterFactory())
.build()
return scarlet.create()
}
}
interface ComponentProvider {
val echoBotComponent: EchoBotComponent
}
}
How can customize the demo app to make my own okhttp WebSocket client?
I am assuming here that you want to use scarlet in a java project. First of all, the version you are using is outdated. Please find the latest version of scarlet library here. It has breaking changes and you will need to migrate your code.
You can use the scarlet library in a java project as is without depending on the kotlin specific modules likescarlet-stream-adapter-coroutines. An example EchoBotModule implementation in java is given below.
@Module
class EchoBotModule {
@Provides
@EchoBotScope
OkHttpClient provideOkHttpClient() {
return OkHttpClient.Builder()
.addInterceptor(HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.BASIC))
.build();
}
@Provides
@EchoBotScope
Lifecycle provideLifecycle(Application application, LoggedInLifecycle loggedInLifecycle) {
return AndroidLifecycle.ofApplicationForeground(application)
.combineWith(loggedInLifecycle);
}
@Provides
@EchoBotScope
EchoService provideEchoService(OkHttpClient client, Lifecycle lifecycle) {
Protocol pr = new OkHttpWebSocket(client, new OkHttpWebSocket.SimpleRequestFactory(
() -> new Request.Builder().url("wss://demos.kaazing.com/echo").build(),
() -> ShutdownReason.GRACEFUL
));
List<MessageAdapter.Factory> messageAdapterFactories = Collections.singletonList(new BitmapMessageAdapter.Factory());
List<StreamAdapter.Factory> streamAdapterFactories = Collections.singletonList(new RxJava2StreamAdapterFactory());
Scarlet.Configuration configuration = new Scarlet.Configuration(lifecycle, null, streamAdapterFactories, messageAdapterFactories, false);
Scarlet scarlet = new Scarlet(pr, configuration);
return scarlet.create();
}
}