Pushing messages via web sockets with akka http

雨燕双飞 提交于 2019-12-24 02:05:31

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!