问题
I am using akka http 2.0.3 for an app and want to use web sockets. I want to be able to push messages from the server to the client, without having to receive a message first. So, I was looking at the UpgradeToWebsocket trait and it looked like using 'handleMessagesWithSinkSource' would be the right thing. Now, for pushing the messages I wanted to have an actor connected to a source which is passed to the 'handleMessagesWithSinkSource' method. However, when using the 'Source.actorRef' method it seems like the actor is only generated when the flow is run, which would be within the 'handleMessagesWithSinkSource' method.
So, my question is, how to push messages to a web-socket channel, preferably via an actor?
回答1:
Found that it is possible to pass an ActorPublisher to a Source:
Source.fromPublisher(ActorPublisher(myActorPublisher)
This way the publisher is first instantiated and then passed to Source which is then passed to 'handleMessagesWithSinkSource'
来源:https://stackoverflow.com/questions/35634283/pushing-messages-via-web-sockets-with-akka-http