如何在Java中使用okhttp实现猩红色的websocket库?

时间:2019-02-20 10:43:54

标签: kotlin websocket okhttp

我想知道Scarlet Web套接字库是否有Java实现。

 @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
    }
}

如何自定义演示应用程序以制作自己的okhttp WebSocket客户端?

1 个答案:

答案 0 :(得分:0)

我在这里假设您想在Java项目中使用Scarlet。首先,您使用的版本已过时。请找到猩红色库here的最新版本。它具有重大更改,您将需要迁移代码。

您可以直接在Java项目中使用Scarlet库,而无需依赖于scarlet-stream-adapter-coroutines之类的kotlin特定模块。下面提供了Java中的EchoBotModule示例实现。

  @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();
    }
}