Simple beginner questions: Accessing values in flows

47 views
Skip to first unread message

mniel...@gmail.com

unread,
Oct 15, 2016, 12:51:10 PM10/15/16
to Akka User List
I have the following code:


val cmdSrc
: Source[Message, ActorRef] = Source.actorRef[Message](10, OverflowStrategy.fail)

val wsr: WebSocketRequest = WebSocketRequest(myEndpoint)

val webSocketFlow: Flow[Message, Message, Future[WebSocketUpgradeResponse]] = Http().webSocketClientFlow(wsr)

val webSocketGraph: RunnableGraph[(ActorRef, Future[Done])] = cmdSrc.via(webSocketFlow).toMat(printSink)(Keep.both)

val (actorRef, fd): (ActorRef, Future[Done]) = webSocketGraph.run()



The idea is that I want to send messages to actorRef that are then sent through the flow.

This code produces the required result, but its an early and amateurish attempt as I try to understand Akka.

My questions are:
  • Is this somewhere near the accepted idiom, or is there a better representation of the same thing?
  • How do I get my hands on the Future[WebSocketUpgradeResponse] from webSocketFlow (line 3)?
Thanks!

mniel...@gmail.com

unread,
Oct 15, 2016, 7:13:26 PM10/15/16
to Akka User List
Actually, I worked it out.  Thanks!

Evgeny Shepelyuk

unread,
Oct 19, 2016, 1:08:37 AM10/19/16
to Akka User List
Hi, could you please spare final code ?

mniel...@gmail.com

unread,
Oct 19, 2016, 4:09:16 PM10/19/16
to Akka User List
So, rightly or wrongly, I did this, which seems to work:

 val flow = builder.add(wsl.mapMaterializedValue(f => {

          f map {

            u => if (u.response.status == StatusCodes.SwitchingProtocols) {

              log.info("Switched protocols")

            }

            else

              throw new IllegalStateException("Did not switch")

          }

})

The key bit is the mapMaterializedValue inside the builder.add.

Whether this is how one is intended to do it is another matter, but so far the result has proven satisfactory.
Reply all
Reply to author
Forward
0 new messages