[英]Sockets and Streaming in Play Framework
I am reading the Play-Socket.io documentation and Play WebSocket documentation on how to use them via Akka Streams. 我正在阅读有关如何通过Akka Streams使用它们的Play-Socket.io 文档和Play WebSocket 文档 。
I cannot understand why it's necessary to use Flow.fromSinkAndSource
. 我不明白为什么需要使用
Flow.fromSinkAndSource
。
The documentation for Play WebSockets says: Play WebSockets的文档说:
Note that while conceptually, a flow is often viewed as something that receives messages, does some processing to them, and then produces the processed messages - there is no reason why this has to be the case, the input of the flow may be completely disconnected from the output of the flow.
请注意,虽然从概念上讲,流通常被视为接收消息,对其进行一些处理然后生成已处理消息的事物-没有理由一定要这样,但流的输入可能会完全断开从流的输出。 Akka streams provides a constructor, Flow.fromSinkAndSource, exactly for this purpose, and often when handling WebSockets, the input and output will not be connected at all.
Akka流为此目的提供了一个构造函数Flow.fromSinkAndSource,通常在处理WebSocket时,输入和输出根本不会连接。
I don't get this at all, why they should be disconnected at all? 我一点都不明白,为什么要将它们断开呢? Why doesn't a simple
Flow
make sense here? 为什么简单的
Flow
在这里没有意义?
I cannot understand why it's necessary to use
Flow.fromSinkAndSource
....I don't get this at all, why they should be disconnected at all?我不明白为什么需要使用
Flow.fromSinkAndSource
....我一点都不明白,为什么要将它们断开? Why doesn't a simpleFlow
make sense here?为什么简单的
Flow
在这里没有意义?
In Play, a WebSocket message handler is a Flow[In, Out, _]
(which is converted to/from a Flow[Message, Message, Any]
via a MessageFlowTransformer
). 在Play中,WebSocket消息处理程序是
Flow[In, Out, _]
(通过MessageFlowTransformer
转换为Flow[Message, Message, Any]
)。 If you're using WebSockets in a request-response fashion, then you could use Flow[In].map
or something with similar semantics, to implement this handler (the following examples are from the Play documentation ): 如果您以请求-响应方式使用WebSockets,则可以使用
Flow[In].map
或具有类似语义的东西来实现此处理程序(以下示例来自Play文档 ):
def socket = WebSocket.accept[String, String] { request =>
// log the message to stdout and send response back to client
Flow[String].map { msg =>
println(msg)
"I received your message: " + msg
} // Flow[String, String, _]
}
In the above example, the input and output are connected: the input is transformed, and the result of that transformation is used as the output. 在上面的示例中,输入和输出连接:输入被转换,转换的结果用作输出。
WebSockets may also be truly bidirectional, meaning that the input and output do not necessarily have to be connected. WebSockets也可能是真正双向的,这意味着输入和输出不一定必须连接。 This is when
Flow.fromSinkAndSource
comes into play: the Sink
and Source
that are passed into this method are independent. 这是
Flow.fromSinkAndSource
起作用的时候:传递到此方法的Sink
和Source
是独立的。 A trivial example of this is a handler that ignores an incoming connection and simply sends a single message to the socket: 一个简单的例子是处理程序,它忽略传入的连接,仅向套接字发送一条消息:
def socket = WebSocket.accept[String, String] { request =>
// Just ignore the input
val in = Sink.ignore
// Send a single 'Hello!' message and close
val out = Source.single("Hello!")
Flow.fromSinkAndSource(in, out) // Flow[String, String, _]
}
In the above example, Flow.fromSinkAndSource
is used because the input and output are not connected: the input has nothing to do with the output. 在上面的示例中,使用
Flow.fromSinkAndSource
的原因是输入和输出未连接:输入与输出无关。
Whether you use WebSockets in a request-response or a truly bidirectional manner, both approaches are modeled as a Flow[In, Out, _]
. 无论您是以请求-响应方式还是以真正的双向方式使用WebSocket,这两种方法都被建模为
Flow[In, Out, _]
。 If you're using WebSockets in a truly bidirectional way, then you can use Flow.fromSinkAndSource
to implement a Flow[In, Out, _]
. 如果您以真正的双向方式使用WebSocket,则可以使用
Flow.fromSinkAndSource
来实现Flow[In, Out, _]
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.