猩红色的Web套接字库连接失败

时间:2019-04-02 15:22:47

标签: android kotlin websocket

仅尝试在Android应用程序(Kotlin)和NodeJS服务器之间实现非常基本的websocket通信。 NodeJS服务器一直在工作,并且可以与其他客户端连接,没有任何问题。我拥有的非常基本的代码如下:

    val client = OkHttpClient.Builder()
        .addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.HBASIC))
        .build()

    if (client != null) {
        val scarlet = Scarlet.Builder()
            .webSocketFactory(client.newWebSocketFactory("ws://192.168.2.33:3000"))
            .addMessageAdapterFactory(MoshiMessageAdapter.Factory())
            .addStreamAdapterFactory(RxJava2StreamAdapterFactory())
            .build()

        val myService = scarlet.create<MyService>()

        myService.observeWebSocketEvent()
            .subscribe { ev ->
                Log.e("WEB SOCKET", ev)
            }
    }

我收到以下错误:

D/OkHttp: --> GET http://192.168.2.33:3000/
D/OkHttp: Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: ctY0bb8jl/mbF6KYGbSJBw==
Sec-WebSocket-Version: 13
--> END GET
D/OkHttp: <-- HTTP FAILED: java.io.IOException: unexpected end of stream on http://192.168.2.33:3000/...

只是想知道我是否做错了什么。我正在使用以下依赖项:

    implementation 'com.github.tinder.scarlet:scarlet:0.1.7'
    implementation 'com.github.tinder.scarlet:scarlet-message-adapter-moshi:0.1.7'
    implementation 'com.github.tinder.scarlet:scarlet-stream-adapter-rxjava2:0.1.7'
    implementation 'com.github.tinder.scarlet:scarlet-websocket-okhttp:0.1.7'
    implementation 'com.squareup.okhttp3:logging-interceptor:3.14.0'

0 个答案:

没有答案