Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

412
Vistas
How to implement scarlet websocket library with okhttp in Java?

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?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

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();
    }
}
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda