如何使用Akka手动将Pong帧发送到WebSockets

问题描述

我有一个用scala-akka编写的websocket客户端,它正在消耗websocket服务器。该服务器每3分钟发送一次ping,并期待pong。乒乓消息也可以未经请求发送。

但是,akka支持自动保持活动状态,但仅在连接空闲时才支持。连接永远不会空闲,但是我想向服务器发送pong帧,否则连接将断开连接。

我试图通过这样的消息来源

val pong = Source.tick(0 minute,1 minute,BinaryMessage.apply(ByteString.apply("0xA")))
//or like this
val pong = Source.tick(0 minute,TextMessage.apply("pong"))

但是它们似乎不起作用,连接完全按照文档中的指定在3分钟后消失。

val flow = Flow.fromSinkAndSourceCoupledMat(printSink,pong)(Keep.both)
val (upgradeResponse,closed) = Http().singleWebSocketRequest(webSocketRequest,flow)

文档中提到这一点的部分:

The websocket server will send a ping frame every 3 minutes. If the websocket server does not receive a pong frame back from the connection within a 10 minute period,the connection will be disconnected. Unsolicited pong frames are allowed.

有什么建议吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)