首页 > 解决方案 > 如何重现 RSocket Kotlin 的错误

问题描述

如何使用 RSocket Kotlin 重现错误以提交错误或在 StackOverflow 上提问。

标签: rsocket

解决方案


在 Intellij 中创建一个 Kotlin 脚本,将其放在任何源文件夹之外,并确保它以.main.kts文件名结尾。

例子.main.kts

#!/usr/bin/env kotlin

@file:Repository("https://repo1.maven.org/maven2/")
@file:Repository("https://jcenter.bintray.com/")
@file:DependsOn("io.rsocket.kotlin:rsocket-core:0.12.0")
@file:DependsOn("io.rsocket.kotlin:rsocket-core-jvm:0.12.0")
@file:DependsOn("io.rsocket.kotlin:rsocket-transport-ktor:0.12.0")
@file:DependsOn("io.rsocket.kotlin:rsocket-transport-ktor-jvm:0.12.0")
@file:DependsOn("io.rsocket.kotlin:rsocket-transport-ktor-client:0.12.0")
@file:DependsOn("io.rsocket.kotlin:rsocket-transport-ktor-client-jvm:0.12.0")
@file:DependsOn("io.ktor:ktor-client-okhttp:1.4.3")
@file:CompilerOptions("-jvm-target", "1.8", "-Xopt-in=kotlin.RequiresOptIn")
@file:OptIn(ExperimentalTime::class)

import io.ktor.client.HttpClient
import io.ktor.client.engine.okhttp.OkHttp
import io.ktor.client.features.websocket.WebSockets
import io.rsocket.kotlin.RSocket
import io.rsocket.kotlin.core.RSocketConnector
import io.rsocket.kotlin.keepalive.KeepAlive
import io.rsocket.kotlin.payload.Payload
import io.rsocket.kotlin.payload.PayloadMimeType
import io.rsocket.kotlin.transport.ktor.client.RSocketSupport
import io.rsocket.kotlin.transport.ktor.client.rSocket
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.take
import kotlinx.coroutines.runBlocking
import kotlin.time.ExperimentalTime
import kotlin.time.seconds

runBlocking {
  val client = HttpClient(OkHttp) {
    install(WebSockets)
    install(RSocketSupport) {
      connector = RSocketConnector {
        connectionConfig {
          // setupPayload(setupPayload)
          keepAlive = KeepAlive(5.seconds)
          payloadMimeType = PayloadMimeType("application/json", "application/json")
        }
      }
    }
  }

  // connect to some url
  val rSocket: RSocket = client.rSocket("wss://rsocket-demo.herokuapp.com/rsocket")

  // request stream
  val stream: Flow<Payload> = rSocket.requestStream(Payload.Empty)

  // take 5 values and print response
  stream.take(5).collect { payload: Payload ->
    println(payload.data.readText())
  }
}

在此处输入图像描述

这 #!行意味着它也会像 shell 脚本一样运行

$ ./example.main.kts
0
1
2
3
4
Exception in thread "DefaultDispatcher-worker-3" java.lang.IllegalArgumentException: End gap 8 is too big: capacity is 7
    at io.ktor.utils.io.core.BufferKt.endGapReservationFailedDueToCapacity(Buffer.kt:463)
    at io.ktor.utils.io.core.Buffer.reserveEndGap(Buffer.kt:220)
    at io.ktor.utils.io.core.AbstractOutput.appendNewChunk(AbstractOutput.kt:195)
    at io.ktor.utils.io.core.AbstractOutput.prepareWriteHead(AbstractOutput.kt:497)
    at io.ktor.utils.io.core.OutputPrimitivesKt.writeIntFallback(OutputPrimitives.kt:133)
    at io.ktor.utils.io.core.OutputPrimitivesKt.writeInt(OutputPrimitives.kt:22)
...

推荐阅读