通过带有akka http的网络套接字推送消息

Ren*_*Web 2 websocket akka akka-stream akka-http

我正在为应用程序使用akka http 2.0.3,并希望使用Web套接字。我希望能够将消息从服​​务器推送到客户端,而不必先接收消息。因此,我正在查看UpgradeToWebsocket特性,看起来使用'handleMessagesWithSinkSource'是正确的选择。现在,为了推送消息,我希望有一个actor连接到传递到'handleMessagesWithSinkSource'方法的源。但是,当使用'Source.actorRef'方法时,似乎仅在流运行时才生成actor,这将在'handleMessagesWithSinkSource'方法中。

因此,我的问题是,如何将消息(最好是通过演员)推送到网络套接字通道?

Ren*_*Web 5

发现可以将ActorPublisher传递给Source:

Source.fromPublisher(ActorPublisher(myActorPublisher)
Run Code Online (Sandbox Code Playgroud)

这样,首先实例化发布者,然后将其传递给Source,然后将其传递给'handleMessagesWithSinkSource'