import play.api.libs.concurrent.Execution.Implicits.defaultContext val futureInt: Future[Int] = scala.concurrent.Future { intensiveComputation()}
import play.api.libs.concurrent.Execution.Implicits.defaultContext def index = Action.async { val futureInt = scala.concurrent.Future { intensiveComputation() } futureInt.map(i => Ok("Got result: " + i))}
import play.api.libs.concurrent.Execution.Implicits.defaultContext import scala.concurrent.duration._ def index = Action.async { val futureInt = scala.concurrent.Future { intensiveComputation() } val timeoutFuture = play.api.libs.concurrent.Promise.timeout("Oops", 1.second) Future.firstCompletedOf(Seq(futureInt, timeoutFuture)).map { case i: Int => Ok("Got result: " + i) case t: String => InternalServerError(t) }}
def index = Action { val file = new java.io.File("/tmp/fileToServe.mov") val fileContent: Enumerator[Array[Byte]] = Enumerator.fromFile(file) SimpleResult( header = ResponseHeader(200, Map(CONTENT_LENGTH -> file.length.toString)), body = fileContent )}
def index = Action { Ok.sendFile(new java.io.File("/tmp/fileToServe.mov"))}
def index = Action { val data = getDataStream val dataContent: Enumerator[Array[Byte]] = Enumerator.fromStream(data) ChunkedResult( header = ResponseHeader(200), chunks = dataContent )}
def index = Action { val data = getDataStream val dataContent: Enumerator[Array[Byte]] = Enumerator.fromStream(data) Ok.chunked(dataContent)}
def index = Action { Ok.chunked( Enumerator("kiki", "foo", "bar").andThen(Enumerator.eof) )}
. , , - , .
, Ok.chunked Ok.stream:
def comet = Action { val events = Enumerator( """<script>console.log('kiki')</script>""", """<script>console.log('foo')</script>""", """<script>console.log('bar')</script>""" ) Ok.stream(events >>> Enumerator.eof).as(HTML)}
: Play framework:
def comet = Action { val events = Enumerator("kiki", "foo", "bar") Ok.stream((events &> Comet(callback = "console.log")) >>> Enumerator.eof)}
WebSockets
Play , Comet sockets . : , , , , Ajax-. , WebSockets ( http://en.wikipedia.org/wiki/WebSocket ), .
, WebSockets, Action: - . WebSocket, Action , , , , HTTP .
WebSocket :
def index = WebSocket.using[String] { request => // Just consume and ignore the input val in = Iteratee.consume[String]() // Send a single 'Hello!' message and close val out = Enumerator("Hello!").andThen(Enumerator.eof) (in, out)}
In - - Iteratee, , ; out - - Enumerator, .
, , , Play - , . : , .
. , , - , .
, Ok.chunked Ok.stream:
def comet = Action { val events = Enumerator( """<script>console.log('kiki')</script>""", """<script>console.log('foo')</script>""", """<script>console.log('bar')</script>""" ) Ok.stream(events >>> Enumerator.eof).as(HTML)}
: Play framework:
def comet = Action { val events = Enumerator("kiki", "foo", "bar") Ok.stream((events &> Comet(callback = "console.log")) >>> Enumerator.eof)}
WebSockets
Play , Comet sockets . : , , , , Ajax-. , WebSockets ( http://en.wikipedia.org/wiki/WebSocket ), .
, WebSockets, Action: - . WebSocket, Action , , , , HTTP .
WebSocket :
def index = WebSocket.using[String] { request => // Just consume and ignore the input val in = Iteratee.consume[String]() // Send a single 'Hello!' message and close val out = Enumerator("Hello!").andThen(Enumerator.eof) (in, out)}
In - - Iteratee, , ; out - - Enumerator, .
, , , Play - , . : , .
. , , - , .
, Ok.chunked Ok.stream:
def comet = Action { val events = Enumerator( """<script>console.log('kiki')</script>""", """<script>console.log('foo')</script>""", """<script>console.log('bar')</script>""" ) Ok.stream(events >>> Enumerator.eof).as(HTML)}
: Play framework:
def comet = Action { val events = Enumerator("kiki", "foo", "bar") Ok.stream((events &> Comet(callback = "console.log")) >>> Enumerator.eof)}
WebSockets
Play , Comet sockets . : , , , , Ajax-. , WebSockets ( http://en.wikipedia.org/wiki/WebSocket ), .
, WebSockets, Action: - . WebSocket, Action , , , , HTTP .
WebSocket :
def index = WebSocket.using[String] { request => // Just consume and ignore the input val in = Iteratee.consume[String]() // Send a single 'Hello!' message and close val out = Enumerator("Hello!").andThen(Enumerator.eof) (in, out)}
In - - Iteratee, , ; out - - Enumerator, .
, , , Play - , . : , .
. , , - , .
, Ok.chunked Ok.stream:
def comet = Action { val events = Enumerator( """<script>console.log('kiki')</script>""", """<script>console.log('foo')</script>""", """<script>console.log('bar')</script>""" ) Ok.stream(events >>> Enumerator.eof).as(HTML)}
: Play framework:
def comet = Action { val events = Enumerator("kiki", "foo", "bar") Ok.stream((events &> Comet(callback = "console.log")) >>> Enumerator.eof)}
WebSockets
Play , Comet sockets . : , , , , Ajax-. , WebSockets ( http://en.wikipedia.org/wiki/WebSocket ), .
, WebSockets, Action: - . WebSocket, Action , , , , HTTP .
WebSocket :
def index = WebSocket.using[String] { request => // Just consume and ignore the input val in = Iteratee.consume[String]() // Send a single 'Hello!' message and close val out = Enumerator("Hello!").andThen(Enumerator.eof) (in, out)}
In - - Iteratee, , ; out - - Enumerator, .
, , , Play - , . : , .
. , , - , .
, Ok.chunked Ok.stream:
def comet = Action { val events = Enumerator( """<script>console.log('kiki')</script>""", """<script>console.log('foo')</script>""", """<script>console.log('bar')</script>""" ) Ok.stream(events >>> Enumerator.eof).as(HTML)}
: Play framework:
def comet = Action { val events = Enumerator("kiki", "foo", "bar") Ok.stream((events &> Comet(callback = "console.log")) >>> Enumerator.eof)}
WebSockets
Play , Comet sockets . : , , , , Ajax-. , WebSockets ( http://en.wikipedia.org/wiki/WebSocket ), .
, WebSockets, Action: - . WebSocket, Action , , , , HTTP .
WebSocket :
def index = WebSocket.using[String] { request => // Just consume and ignore the input val in = Iteratee.consume[String]() // Send a single 'Hello!' message and close val out = Enumerator("Hello!").andThen(Enumerator.eof) (in, out)}
In - - Iteratee, , ; out - - Enumerator, .
, , , Play - , . : , .
. , , - , .
, Ok.chunked Ok.stream:
def comet = Action { val events = Enumerator( """<script>console.log('kiki')</script>""", """<script>console.log('foo')</script>""", """<script>console.log('bar')</script>""" ) Ok.stream(events >>> Enumerator.eof).as(HTML)}
: Play framework:
def comet = Action { val events = Enumerator("kiki", "foo", "bar") Ok.stream((events &> Comet(callback = "console.log")) >>> Enumerator.eof)}
WebSockets
Play , Comet sockets . : , , , , Ajax-. , WebSockets ( http://en.wikipedia.org/wiki/WebSocket ), .
, WebSockets, Action: - . WebSocket, Action , , , , HTTP .
WebSocket :
def index = WebSocket.using[String] { request => // Just consume and ignore the input val in = Iteratee.consume[String]() // Send a single 'Hello!' message and close val out = Enumerator("Hello!").andThen(Enumerator.eof) (in, out)}
In - - Iteratee, , ; out - - Enumerator, .
, , , Play - , . : , .
. , , - , .
, Ok.chunked Ok.stream:
def comet = Action { val events = Enumerator( """<script>console.log('kiki')</script>""", """<script>console.log('foo')</script>""", """<script>console.log('bar')</script>""" ) Ok.stream(events >>> Enumerator.eof).as(HTML)}
: Play framework:
def comet = Action { val events = Enumerator("kiki", "foo", "bar") Ok.stream((events &> Comet(callback = "console.log")) >>> Enumerator.eof)}
WebSockets
Play , Comet sockets . : , , , , Ajax-. , WebSockets ( http://en.wikipedia.org/wiki/WebSocket ), .
, WebSockets, Action: - . WebSocket, Action , , , , HTTP .
WebSocket :
def index = WebSocket.using[String] { request => // Just consume and ignore the input val in = Iteratee.consume[String]() // Send a single 'Hello!' message and close val out = Enumerator("Hello!").andThen(Enumerator.eof) (in, out)}
In - - Iteratee, , ; out - - Enumerator, .
, , , Play - , . : , .
. , , - , .
, Ok.chunked Ok.stream:
def comet = Action { val events = Enumerator( """<script>console.log('kiki')</script>""", """<script>console.log('foo')</script>""", """<script>console.log('bar')</script>""" ) Ok.stream(events >>> Enumerator.eof).as(HTML)}
: Play framework:
def comet = Action { val events = Enumerator("kiki", "foo", "bar") Ok.stream((events &> Comet(callback = "console.log")) >>> Enumerator.eof)}
WebSockets
Play , Comet sockets . : , , , , Ajax-. , WebSockets ( http://en.wikipedia.org/wiki/WebSocket ), .
, WebSockets, Action: - . WebSocket, Action , , , , HTTP .
WebSocket :
def index = WebSocket.using[String] { request => // Just consume and ignore the input val in = Iteratee.consume[String]() // Send a single 'Hello!' message and close val out = Enumerator("Hello!").andThen(Enumerator.eof) (in, out)}
In - - Iteratee, , ; out - - Enumerator, .
, , , Play - , . : , .
. , , - , .
, Ok.chunked Ok.stream:
def comet = Action { val events = Enumerator( """<script>console.log('kiki')</script>""", """<script>console.log('foo')</script>""", """<script>console.log('bar')</script>""" ) Ok.stream(events >>> Enumerator.eof).as(HTML)}
: Play framework:
def comet = Action { val events = Enumerator("kiki", "foo", "bar") Ok.stream((events &> Comet(callback = "console.log")) >>> Enumerator.eof)}
WebSockets
Play , Comet sockets . : , , , , Ajax-. , WebSockets ( http://en.wikipedia.org/wiki/WebSocket ), .
, WebSockets, Action: - . WebSocket, Action , , , , HTTP .
WebSocket :
def index = WebSocket.using[String] { request => // Just consume and ignore the input val in = Iteratee.consume[String]() // Send a single 'Hello!' message and close val out = Enumerator("Hello!").andThen(Enumerator.eof) (in, out)}
In - - Iteratee, , ; out - - Enumerator, .
, , , Play - , . : , .
. , , - , .
, Ok.chunked Ok.stream:
def comet = Action { val events = Enumerator( """<script>console.log('kiki')</script>""", """<script>console.log('foo')</script>""", """<script>console.log('bar')</script>""" ) Ok.stream(events >>> Enumerator.eof).as(HTML)}
: Play framework:
def comet = Action { val events = Enumerator("kiki", "foo", "bar") Ok.stream((events &> Comet(callback = "console.log")) >>> Enumerator.eof)}
WebSockets
Play , Comet sockets . : , , , , Ajax-. , WebSockets ( http://en.wikipedia.org/wiki/WebSocket ), .
, WebSockets, Action: - . WebSocket, Action , , , , HTTP .
WebSocket :
def index = WebSocket.using[String] { request => // Just consume and ignore the input val in = Iteratee.consume[String]() // Send a single 'Hello!' message and close val out = Enumerator("Hello!").andThen(Enumerator.eof) (in, out)}
In - - Iteratee, , ; out - - Enumerator, .
, , , Play - , . : , .
Source: https://habr.com/ru/post/219471/
All Articles