简体   繁体   English

如何在akka-http的websocket处理程序中注入保持活动状态?

[英]how can I inject a keep-alive into a websocket handler in akka-http?

I've written some code to communicate with a browser over websockets using akka-http 10.0.9. 我已经编写了一些代码,以使用akka-http 10.0.9通过websocket与浏览器进行通信。 It works nicely, except that the connection times out. 除了连接超时外,它的运行效果很好。 Although we're sending a constant stream of data to the client, the client isn't requesting anything for long periods of time. 尽管我们正在向客户端发送恒定的数据流,但是客户端长时间不请求任何内容。

I know the solution is to inject keep-alive messages into the incoming websocket source, like this: 我知道解决方案是将保持活动消息注入传入的Websocket源,如下所示:

val s = Source(List(data....)).keepAlive(15.seconds, () => Strict.TextMessage("heart beat"))

However, the akka-http api calls for a flow to handle the connection: 但是,akka-http api要求一个流来处理连接:

path("query") {
        parameter('session, 'package) {
          (session,packageId) =>
            val sg = getGraphManager(session)
            sg match {
              case Left(error) => complete(StatusCodes.BadRequest -> TSError(msg=error))
              case Right(gm) =>
                val tsFlow = new TSFlow(ksource, session)
                handleWebSocketMessages(Flow.fromGraph(tsFlow.flowGraph))

            }
         }
 }

Consequently, I never see the incoming source, so there's no opportunity to inject the keep-alive. 因此,我从未看到传入的源,因此没有机会注入keep-alive。 How should I restructure things so I can inject a keep-alive? 我应该如何重组事物,以便注入保持活动状态?

The only way to control how connections are dropped server-side is to change the idle-timeout (see the docs here and here ). 控制如何断开服务器端连接的唯一方法是更改idle-timeout (请参阅此处此处的文档)。

You can set 你可以设定

idle-timeout = infinite

to completely disable the timeout. 完全禁用超时。

If you want to use keep alive messages, they need to be sent from the client side. 如果要使用保持活动消息,则需要从客户端发送它们。 The .keepAlive combinator is the easiest way to get them if your client is based on Akka Streams. 如果您的客户端基于Akka Streams,则.keepAlive组合器是获取它们的最简单方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM