gpt4 book ai didi

android - 无法使用 Tinder Scarlet 连接到 ws

转载 作者:行者123 更新时间:2023-12-02 12:38:18 27 4
gpt4 key购买 nike

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

日志:

D/Connection$connect:应该连接

D/Connection$connect: WaitingToRetry

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

D/Connection$connect:应该连接

D/Connection$connect: 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>
}

最佳答案

您实际上可以通过添加 android:usesCleartextTraffic="true" 将 Scarlet 与 WS://一起使用。到您的 list 文件。

关于android - 无法使用 Tinder Scarlet 连接到 ws,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57675908/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com