【问题标题】:How to implement scarlet websocket library with okhttp in Java?如何在 Java 中使用 okhttp 实现猩红色 websocket 库?
【发布时间】:2019-07-14 00:41:39
【问题描述】:

我想知道 Scarlet web socket 库是否有 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
    }
}

如何自定义demo app来制作自己的okhttp WebSocket客户端?

【问题讨论】:

    标签: kotlin websocket okhttp


    【解决方案1】:

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

    您可以在 Java 项目中按原样使用 Scarlet 库,而无需依赖于 kotlin 特定模块,例如scarlet-stream-adapter-coroutines。下面给出了一个例子EchoBotModule在java中的实现。

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

    【讨论】:

    • 嘿,如果你实现了,你能提供完整的java示例吗?
    • 对不起,我没有完整的 java 实现。但是,如果您在此过程中遇到任何技术瓶颈,我们很乐意提供帮助。
    猜你喜欢
    • 2013-11-28
    • 2018-09-05
    • 2020-04-23
    • 1970-01-01
    • 2015-11-10
    • 2014-08-07
    • 1970-01-01
    相关资源
    最近更新 更多