Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
P
play-scala-seed
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
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
COSC360 in 2018
play-scala-seed
Commits
d3dabc94
Commit
d3dabc94
authored
6 years ago
by
Will Billingsley
Browse files
Options
Downloads
Patches
Plain Diff
Made one of the websocket examples more idiomatic
parent
70d8eeb1
Branches
master
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/controllers/ExamplesController.scala
+29
-23
29 additions, 23 deletions
app/controllers/ExamplesController.scala
conf/routes
+2
-2
2 additions, 2 deletions
conf/routes
with
31 additions
and
25 deletions
app/controllers/ExamplesController.scala
+
29
−
23
View file @
d3dabc94
...
...
@@ -87,7 +87,7 @@ class ExamplesController @Inject()(cc: ControllerComponents)(implicit ec: Execut
*
* @return
*/
def
web
s
ocketWithHelper
()
:
WebSocket
=
{
def
web
S
ocketWithHelper
()
:
WebSocket
=
{
WebSocketHelper
.
createWebSocket
[
String
,
String
](
{
queue
=>
// We now have an asynchronous queue. What would we like to do with it
...
...
@@ -109,39 +109,44 @@ class ExamplesController @Inject()(cc: ControllerComponents)(implicit ec: Execut
* asynchronous work. However, using this template, you should just
* @return
*/
def
websocket
()
=
WebSocket
.
accept
[
String
,
String
]
{
request
=>
// Our source is going to be an asynchronous queue we can push messages to with 'out.offer(msg)'. This is going to
// be the "source" of our flow. A complication is it doesn't create the queue immediately - this just says
// *when* this source is connected to a flow, create a queue. So we don't have a queue yet.
val
outSource
=
Source
.
queue
[
String
](
50
,
OverflowStrategy
.
backpressure
)
def
webSocket
()
=
WebSocket
.
accept
[
String
,
String
]
{
request
=>
// Because I want to refer to the queue in the sink, but I haven't actually got the queue yet, I'm just creating
// a box to put our queue in. At the moment, it's empty
var
outOpt
:
Option
[
SourceQueueWithComplete
[
String
]]
=
None
//
Create a "sink" that describes what we want to do with each message. Here, I'm just going to count the charact
er
s
//
and echo it straight back out on the "out" queue
.
val
in
=
Sink
.
foreach
[
String
]
{
message
=>
//
A websocket is made from a "flow" - a source of messages up to the browser, and a sink for messages from the brows
er
.
//
We're going to need to give this function our Sink and our Source as arguments
.
Flow
.
fromSinkAndSourceCoupledMat
(
// If we have an out queue, send the message on it
outOpt
foreach
{
out
=>
out
.
offer
(
s
"That message had ${message.length} characters in it"
)
}
// Create a "sink" that describes what we want to do with each message. Here, I'm just going to count the characters
// and echo it straight back out on the "out" queue.
Sink
.
foreach
[
String
]
{
message
=>
}
// If we have an out queue, send the message on it
outOpt
foreach
{
out
=>
out
.
offer
(
s
"That message had ${message.length} characters in it"
)
}
},
// This defines a "flow" -- something that has an input and an output.
// "Coupled" means that if the input closes, the output will close
// "Mat" means "materialised" -- ie, it'll give us the output queue that gets created and the Future that will
// complete when the flow is done.
Flow
.
fromSinkAndSourceCoupledMat
(
in
,
outSource
)
{
case
(
done
,
out
)
=>
// Our source is going to be an asynchronous queue we can push messages to with 'out.offer(msg)'.
// The queue will be created when the connection is complete - this just says what it *will* be
Source
.
queue
[
String
](
50
,
OverflowStrategy
.
backpressure
)
// done is a future that will complete when the flow finishes
)
{
case
(
done
,
queue
)
=>
// the second argument list is a callback that takes a tuple with two arguments.
// done is a future (like a JavaScript promise) that will complete when the flow finishes
// out is our "materialised" output queue. All we need do is put it in the box we made earlier
out
.
offer
(
"Connected!"
)
outOpt
=
Some
(
out
)
// Send a connected message to the browser
queue
.
offer
(
"Connected!"
)
// Put the queue in the box we defined earlier
outOpt
=
Some
(
queue
)
// When the connection has ended, empty the box and println that it's closed
done
.
foreach
{
_
=>
outOpt
=
None
println
(
"Connection closed!"
)
...
...
@@ -149,6 +154,7 @@ class ExamplesController @Inject()(cc: ControllerComponents)(implicit ec: Execut
}
}
/**
...
...
This diff is collapsed.
Click to expand it.
conf/routes
+
2
−
2
View file @
d3dabc94
...
...
@@ -11,8 +11,8 @@ POST /example/json controllers.ExamplesController.receiveJsonPo
GET /example/game controllers.ExamplesController.game(id:Option[String])
GET /example/socket controllers.ExamplesController.web
s
ocket()
GET /example/time controllers.ExamplesController.web
s
ocketWithHelper()
GET /example/socket controllers.ExamplesController.web
S
ocket()
GET /example/time controllers.ExamplesController.web
S
ocketWithHelper()
GET /example/eventsource controllers.ExamplesController.eventSource()
...
...
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