Lift Cookbook by Richard Dallaway
Author:Richard Dallaway [Richard Dallaway]
Language: eng
Format: epub, pdf
Tags: COMPUTERS / Programming Languages / Java
ISBN: 9781449362676
Publisher: O’Reilly Media
Published: 2013-06-23T16:00:00+00:00
See Also
Custom 404 Page describes how to create a custom 404 (not found) page.
Streaming Content
Problem
You want to stream content back to the web client.
Solution
Use OutputStreamResponse, passing it a function that will write to the OutputStream that Lift supplies.
In this example, we’ll stream all the integers from one, via a REST service:
package code.rest import net.liftweb.http.{Req,OutputStreamResponse} import net.liftweb.http.rest._ object Numbers extends RestHelper { // Convert a number to a String, and then to UTF-8 bytes // to send down the output stream. def num2bytes(x: Int) = (x + "\n") getBytes("utf-8") // Generate numbers using a Scala stream: def infinite = Stream.from(1).map(num2bytes) serve { case Req("numbers" :: Nil, _, _) => OutputStreamResponse( out => infinite.foreach(out.write) ) } }
Scala’s Stream class is a way to generate a sequence with lazy evaluation. The values being produced by infinite are used as example data to stream back to the client.
Wire this into Lift in Boot.scala:
LiftRules.dispatch.append(Numbers)
Visiting http://127.0.0.1:8080/numbers will generate a 200 status code and start producing the integers from 1. The numbers are produced quite quickly, so you probably don’t want to try that in your web browser, but instead from something that is easier to stop, such as cURL.
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
Deep Learning with Python by François Chollet(12960)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10415)
Hello! Python by Anthony Briggs(10202)
The Mikado Method by Ola Ellnestam Daniel Brolund(10110)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(10047)
Dependency Injection in .NET by Mark Seemann(9587)
Hit Refresh by Satya Nadella(9020)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8592)
The Kubernetes Operator Framework Book by Michael Dame(8395)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8198)
Robo-Advisor with Python by Aki Ranin(8155)
Practical Computer Architecture with Python and ARM by Alan Clements(8140)
Implementing Enterprise Observability for Success by Manisha Agrawal and Karun Krishnannair(8116)
Building Low Latency Applications with C++ by Sourav Ghosh(8007)
Svelte with Test-Driven Development by Daniel Irvine(8002)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(7971)
Grails in Action by Glen Smith Peter Ledbrook(7944)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7923)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7904)