[英]Streaming data to/from Play framework on an open connection
I need to send a stream of data to Play server. 我需要将数据流发送到Play服务器。 The length of the stream is unknown and I need to get a response every line break
\\n
or for every several lines. 流的长度是未知的,我需要在每个换行符
\\n
或每几行得到一个响应。 Rather then wait for the whole data to be sent. 而是等待整个数据发送出去。
Think of the following usecase: 考虑以下用例:
lets say i'm intended to write a console application, that when launched, connects to my web server, and all the user input are being sent to play on every line break, and gets responded asynchronously. 可以说我打算编写一个控制台应用程序,该应用程序在启动时将连接到我的Web服务器,并且所有用户输入都将在每个换行符上发送以进行播放,并得到异步响应。 All above should be performed on a single connection, ie I don't want to open a new connection on every request I send to Play (a good analog would be 2 processes communicating through 2 pipes) .
以上所有操作都应在单个连接上执行,即我不想在我发送给Play的每个请求上打开新连接(一个很好的模拟是2个进程通过2个管道进行通信) 。
What is the best way to achieve this? 实现此目标的最佳方法是什么?
And is it possible to achieve with a client that communicates with the server only via http (with a single http connection)? 是否可以通过仅通过http(具有单个http连接)与服务器进行通信的客户端来实现?
EDIT: 编辑:
my current thoughts on how to approach this are as follows: 我目前对如何处理此问题的想法如下:
i can define a new BodyParser[Future[String]]
which is basically an Iteratee[Array[Byte],Future[String]]
. 我可以定义一个新的
BodyParser[Future[String]]
,它基本上是一个Iteratee[Array[Byte],Future[String]]
。 while the parsing takes place, i can compute the result asynchronously and the action can return the result as ChunkedResult
in the future's onComplete
method. 在进行解析时,我可以异步计算结果,并且该操作可以在将来的
onComplete
方法中将结果作为ChunkedResult
返回。
does this sound like the right approach? 这听起来像是正确的方法吗?
any suggestions on how to achieve this? 关于如何实现这一目标的任何建议?
Maybe you should look at websockets. 也许您应该看看websockets。 Java: http://www.playframework.com/documentation/2.1-RC3/JavaWebSockets Scala: http://www.playframework.com/documentation/2.0/ScalaWebSockets
Java: http: //www.playframework.com/documentation/2.1-RC3/JavaWebSockets Scala: http : //www.playframework.com/documentation/2.0/ScalaWebSockets
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.