如何复制 RSocket Kotlin 的错误

问题描述

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

解决方法

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

example.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())
  }
}

enter image description here

#! line 表示它也将像 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)
...
,

运行 rsocket-cli 以向 rsocket 服务器发送测试请求

$ brew install yschimke/tap/rsocket-cli
$ rsocket-cli wss://rsocket-demo-1.ew.r.appspot.com/rsocket --route timer --stream -i '{"a": "b"}' --requestn 5
0
1
2

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...