我是Akka http的新手。我使用最新的稳定请求版本2.4.7。 我试图连接到websocket,我能够这样做。问题是当我使用“wss://s1.ripple.com”使用它时,握手后连接关闭。
val incoming: Sink[Message, Future[Done]] =
Sink.foreach[Message] {
case message: TextMessage.Strict =>
println(message.text)
case message:TextMessage.Streamed =>
println(message)
case message => println
}
val outgoing = Source.single(TextMessage("""{"command" : "fee"}"""))
val webSocketFlow = Http().webSocketClientFlow(WebSocketRequest("wss://echo.websocket.org"))
val (upgradeResponse, closed) = outgoing
.viaMat(webSocketFlow)(Keep.right)
.toMat(incoming)(Keep.both)
.run()
val connected = upgradeResponse.flatMap { upgrade =>
if (upgrade.response.status == StatusCodes.OK || upgrade.response.status == StatusCodes.SwitchingProtocols) {
Future.successful(Done)
} else {
throw new RuntimeException(s"Connection failed: ${upgrade.response.status}")
}
}
connected.onComplete(r => println(s"camote ${r} ${upgradeResponse}"))
closed.foreach(_ => println("closed"))
/ ============================================== ====更新=== 我现在可以从服务器获取消息。似乎添加了Source.empty就可以了。但现在的问题是我无法断开与服务器的连接... Source.empty使连接保持活动状态直到超时。我可以从客户端手动关闭与服务器的连接吗?
val source=Source.single(TextMessage.Strict(<JsStringMessage>)).concatMat(Source.maybe)(Keep.left)
val source =
Http().webSocketClientFlow(WebSocketRequest("<wss url>"))
val (done,data)= handshaker
.viaMat(webSocketFlow)(Keep.right)
.toMat(Sink.last)(Keep.both)
.run()
val resuldata:Future[String] = data.map{
case message:TextMessage.Strict => message.text
case message =>"""{"status" : "failed"}"""
}
resuldata