Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
L
lavamaze
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Will Billingsley
lavamaze
Commits
70d8eeb1
Commit
70d8eeb1
authored
6 years ago
by
Will Billingsley
Browse files
Options
Downloads
Patches
Plain Diff
Added EventSource example
parent
b1266280
No related branches found
No related tags found
No related merge requests found
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/controllers/ExamplesController.scala
+20
-1
20 additions, 1 deletion
app/controllers/ExamplesController.scala
conf/routes
+2
-0
2 additions, 0 deletions
conf/routes
with
22 additions
and
1 deletion
app/controllers/ExamplesController.scala
+
20
−
1
View file @
70d8eeb1
...
...
@@ -3,7 +3,8 @@ package controllers
import
javax.inject._
import
akka.stream.
{
Materializer
,
OverflowStrategy
,
ThrottleMode
}
import
akka.stream.scaladsl.
{
Flow
,
Sink
,
Source
,
SourceQueueWithComplete
}
import
akka.stream.scaladsl.
{
Flow
,
Keep
,
Sink
,
Source
,
SourceQueueWithComplete
}
import
play.api.libs.EventSource
import
scala.concurrent.duration._
import
scala.util.Random
...
...
@@ -150,6 +151,24 @@ class ExamplesController @Inject()(cc: ControllerComponents)(implicit ec: Execut
}
/**
* An example using EventSource (server sent events).
* @return
*/
def
eventSource
()
=
Action
{
request
=>
// The source "will be" a queue we can push to. But this doesn't create the queue yet
val
s
=
Source
.
queue
[
String
](
50
,
OverflowStrategy
.
backpressure
)
// Connect the source via the EventSource's flow.
val
stream
=
s
.
viaMat
(
EventSource
.
flow
[
String
])
{
// It calls us back when the queue has been created!
case
(
queue
,
m
)
=>
listeners
.
add
(
queue
)
m
}
Ok
.
chunked
(
stream
).
as
(
"text/event-stream"
)
}
}
This diff is collapsed.
Click to expand it.
conf/routes
+
2
−
0
View file @
70d8eeb1
...
...
@@ -14,6 +14,8 @@ GET /example/game controllers.ExamplesController.game(id:Optio
GET /example/socket controllers.ExamplesController.websocket()
GET /example/time controllers.ExamplesController.websocketWithHelper()
GET /example/eventsource controllers.ExamplesController.eventSource()
# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.versioned(path="/public", file: Asset)
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment