无法使用Tinder Scarlet连接到ws

时间:2019-08-27 13:36:38

标签: android kotlin websocket

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

日志:

D / Connection $ connect:应该连接

D /连接$连接:WaitingToRetry

D / Connection $ connect:WebSocket连接失败

D / Connection $ connect:应该连接

D /连接$连接:WaitingToRetry

D / Connection $ connect:WebSocket连接失败

有我的代码:

在MainActivity中

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    Connection.connect()
}

对象(单个)

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


    }
}

接口

interface WebSocketService {
@Receive
fun observeStateTransition(): Flowable<StateTransition>

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

2 个答案:

答案 0 :(得分:2)

实际上,可以通过在清单文件中添加android:usesCleartextTraffic="true"来将Scarlet与WS://一起使用。

答案 1 :(得分:1)

问题是我使用了ws://,但是据我所知,Scarlet仅适用于wss:/