首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法使用Tinder Scarlet连接到% ws

无法使用Tinder Scarlet连接到% ws
EN

Stack Overflow用户
提问于 2019-08-27 21:36:38
回答 2查看 1.5K关注 0票数 2

正在尝试测试与% ws的连接,但服务器看不到任何连接。我想我错过了什么,你能告诉我我错过了什么吗?

日志:

D/Connection$ Connect :应该连接

D/连接$connect: WaitingToRetry

D/ Connection $connect:在WebSocket连接上失败

D/Connection$ Connect :应该连接

D/连接$connect: WaitingToRetry

D/ Connection $connect:在WebSocket连接上失败

下面是我的代码:

在MainActivity中

代码语言:javascript
复制
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    Connection.connect()
}

对象(单例)

代码语言:javascript
复制
object Connection {
    fun connect() {

        val okHttpClient = OkHttpClient.Builder()
            .readTimeout(0, TimeUnit.MILLISECONDS)
            .build()

        val protocol = OkHttpWebSocket(
            okHttpClient,
            OkHttpWebSocket.SimpleRequestFactory(
                { Request.Builder().url("ws://someURL").build() },
                { ShutdownReason.GRACEFUL }
            )
        )

        val configuration = Scarlet.Configuration(
            streamAdapterFactories = listOf(RxJava2StreamAdapterFactory())
        )

        val scarletInstance = Scarlet(protocol, configuration)

        val webSocketService = scarletInstance.create<WebSocketService>()

    webSocketService.observeStateTransition()
        .observeOn(Schedulers.io())
        .subscribe({ stateTransition ->
            val event = stateTransition.event
            val description = when (event) {
                is Event.OnLifecycleStateChange -> when (event.lifecycleState) {
                    LifecycleState.Started -> Timber.d("On Lifecycle Start")
                    LifecycleState.Stopped -> Timber.d("On Lifecycle Stop")
                    LifecycleState.Completed -> Timber.d("On Lifecycle Terminate")
                }
                is Event.OnProtocolEvent -> {
                    when (stateTransition.toState) {
                        is State.WillConnect -> Timber.d("WaitingToRetry")
                        is State.Connecting -> Timber.d("Connecting")
                        is State.Connected -> Timber.d("Connected")
                        is State.Disconnecting -> Timber.d("Disconnecting")
                        State.Disconnected -> Timber.d("Disconnected")
                        State.Destroyed -> Timber.d("Destroyed")
                    }
                }
                Event.OnShouldConnect -> Timber.d("Should Connect")
            }
        }, {
            e ->
            Timber.e(e)
        })

    webSocketService.observeWebSocketEvent()
        .observeOn(Schedulers.io())
        .subscribe({
            event ->
            val description = when(event){
                is WebSocketEvent.OnConnectionOpened -> Timber.d("On WebSocket Connection Opened")
                is WebSocketEvent.OnMessageReceived -> Timber.d("On WebSocket NewMessageUpdate Received")
                is WebSocketEvent.OnConnectionClosing -> Timber.d("On WebSocket Connection Closing")
                is WebSocketEvent.OnConnectionClosed -> Timber.d("On WebSocket Connection Closed")
                is WebSocketEvent.OnConnectionFailed -> Timber.d("On WebSocket Connection Failed")
            }
        },
            {
                Timber.e(it)
            })


    }
}

接口

代码语言:javascript
复制
interface WebSocketService {
@Receive
fun observeStateTransition(): Flowable<StateTransition>

@Receive
fun observeWebSocketEvent(): Flowable<WebSocketEvent>
}
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-09-07 06:51:34

通过将android:usesCleartextTraffic="true"添加到清单文件中,您可以通过WS://实际使用Scarlet。

票数 3
EN

Stack Overflow用户

发布于 2019-08-28 18:58:40

问题是我使用的是ws://,但我认识到Scarlet只适用于wss:/

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57675908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档