开始使用akka-streams,我想构建一个简单的示例。在chrome中,使用web套接字插件,我可以通过wss://ws.blockchain.info/inv
并发送2个命令,简单地连接到这样的流https://blockchain.info/api/apiwebsocket
{“op”:“ping”}
{“op”:“unconfirmed_sub”}
将在chromes web socket插件窗口中传输结果。我试图在akka流中实现相同的功能,但面临一些问题:
在学习http://doc.akka.io/docs/akka/2.4.7/scala/http/client-side/websocket-support.html或http://doc.akka.io/docs/akka-http/10.0.0/scala/http/client-side/websocket-support.html#half-closed-client-websockets教程时,以下是我的修改:
object SingleWebSocketRequest extends App {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
import system.dispatcher
// print each incoming strict text message
val printSink: Sink[Message, Future[Done]] =
Sink.foreach {
case message: TextMessage.Strict =>
println(message.text)
}
val commandMessages = Seq(TextMessage("{\"op\":\"ping\"}"), TextMessage("{\"op\":\"unconfirmed_sub\"}"))
val helloSource: Source[Message, NotUsed] = Source(commandMessages.to[scala.collection.immutable.Seq])
// the Future[Done] is the materialized value of Sink.foreach
// and it is completed when the stream completes
val flow: Flow[Message, Message, Future[Done]] =
Flow.fromSinkAndSourceMat(printSink, helloSource)(Keep.left)
// upgradeResponse is a Future[WebSocketUpgradeResponse] that
// completes or fails when the connection succeeds or fails
// and closed is a Future[Done] representing the stream completion from above
val (upgradeResponse, closed) =
Http().singleWebSocketRequest(WebSocketRequest("wss://ws.blockchain.info/inv"), flow)
val connected = upgradeResponse.map { upgrade =>
// just like a regular http request we can access response status which is available via upgrade.response.status
// status code 101 (Switching Protocols) indicates that server support WebSockets
if (upgrade.response.status == StatusCodes.SwitchingProtocols) {
Done
} else {
throw new RuntimeException(s"Connection failed: ${upgrade.response.status}")
}
}
// in a real application you would not side effect here
// and handle errors more carefully
connected.onComplete(println) // TODO why do I not get the same output as in chrome?
closed.foreach(_ => println("closed"))
}
{"op":"pong"}
{"op":"pong"}
请参阅代码:
object WebSocketClientFlow extends App {
implicit val system = ActorSystem()
implicit val materializer = ActorMaterializer()
import system.dispatcher
// Future[Done] is the materialized value of Sink.foreach,
// emitted when the stream completes
val incoming: Sink[Message, Future[Done]] =
Sink.foreach[Message] {
case message: TextMessage.Strict =>
println(message.text)
}
// send this as a message over the WebSocket
val commandMessages = Seq(TextMessage("{\"op\":\"ping\"}"), TextMessage("{\"op\":\"unconfirmed_sub\"}"))
val outgoing: Source[Message, NotUsed] = Source(commandMessages.to[scala.collection.immutable.Seq])
// val outgoing = Source.single(TextMessage("hello world!"))
// flow to use (note: not re-usable!)
val webSocketFlow = Http().webSocketClientFlow(WebSocketRequest("wss://ws.blockchain.info/inv"))
// the materialized value is a tuple with
// upgradeResponse is a Future[WebSocketUpgradeResponse] that
// completes or fails when the connection succeeds or fails
// and closed is a Future[Done] with the stream completion from the incoming sink
val (upgradeResponse, closed) =
outgoing
.viaMat(webSocketFlow)(Keep.right) // keep the materialized Future[WebSocketUpgradeResponse]
.toMat(incoming)(Keep.both) // also keep the Future[Done]
.run()
// just like a regular http request we can access response status which is available via upgrade.response.status
// status code 101 (Switching Protocols) indicates that server support WebSockets
val connected = upgradeResponse.flatMap { upgrade =>
if (upgrade.response.status == StatusCodes.SwitchingProtocols) {
Future.successful(Done)
} else {
throw new RuntimeException(s"Connection failed: ${upgrade.response.status}")
}
}
// in a real application you would not side effect here
connected.onComplete(println)
closed.foreach(_ => {
println("closed")
system.terminate
})
}
如何才能达到与chrome相同结果
{“op”:“ping”}
消息我注意到的几件事是:
1)您需要使用所有类型的传入消息,而不仅仅是textmessage.strict
类型。blockchain流绝对是一个流消息,因为它包含大量文本,并且将在网络上以块形式传递。更完整的传入接收器可以是:
val incoming: Sink[Message, Future[Done]] =
Flow[Message].mapAsync(4) {
case message: TextMessage.Strict =>
println(message.text)
Future.successful(Done)
case message: TextMessage.Streamed =>
message.textStream.runForeach(println)
case message: BinaryMessage =>
message.dataStream.runWith(Sink.ignore)
}.toMat(Sink.last)(Keep.right)
2)您的2个元素的源可能完成得太早,即在websocket响应返回之前。您可以通过执行以下操作来连接源.maybe
val outgoing: Source[Strict, Promise[Option[Nothing]]] =
Source(commandMessages.to[scala.collection.immutable.Seq]).concatMat(Source.maybe)(Keep.right)
val ((completionPromise, upgradeResponse), closed) =
outgoing
.viaMat(webSocketFlow)(Keep.both)
.toMat(incoming)(Keep.both)
.run()
我有一个Tomcat7应用程序,使用“长轮询”,为数万个并发连接提供服务。long-polling(简而言之)意味着在向客户机发回响应(新数据或304代码)之前,我将请求保留很长时间。 > 套接字缓冲区-我猜是OS套接字缓冲区,对吗? 在事物的宏伟计划中,这些缓冲站在哪里?它们的意义是什么? 在哪里控制Tomcat的最大套接字数?它是MaxThreads的函数吗?一对一? 如果我去降低它们的值,什
我有一个web服务器,它接受传入的websocket连接,在Scala中用akka http实现。然而,我一直观察到我的应用程序的内存使用量单调增加。经过长时间的挖掘,我发现每个连接都会创建一些内部Akka对象,但在客户机断开连接后没有得到清理。特别是这个类:。每个连接都会创建一个这样的新对象。我使用来计算对象的数量,下面提供了命令。我不确定我是不是做错了什么。任何建议都将不胜感激。 我有一个超级
在过去的几周里,我们一直在关注这个问题。 每隔几天,我们compose.ioMongo数据库的连接数就会飙升至近5000,这是连接限制。似乎没有任何特定的触发此行为。 日志如下所示: Compose支持告诉我们,这几乎肯定是MongoDB驱动程序的问题,我们应该使用连池。我们需要实现Meteor连接池的一些配置吗?
我正在尝试实现一个简单的actor,它将使用akka zeromq从zeromq接收消息。 系统只打印 发送消息是通过一个简单的python完成的 我注意到的另一件奇怪的事情是,按下enter键后,程序不会终止。如果我使用python脚本向它发送一条消息,它将被激活。以下是完整的输出: 使用 是版本(在Arch Linux上) 这是
我似乎找不到一个像样的例子来说明如何通过Python使用AWS动态流。有人能给我举几个例子吗? 最好的