【发布时间】:2019-01-08 18:24:38
【问题描述】:
我有一个akka http web socket Route,其代码类似于:
private val wsReader: Route = 路径(“v1”/“数据”/“ws”){ log.info("正在打开 websocket 连接...")
val testSource = Source
.repeat("Hello")
.throttle(1, 1.seconds)
.map(x => {
println(x)
x
})
.map(TextMessage.Strict)
.limit(1000)
extractUpgradeToWebSocket { upgrade ⇒
complete(upgrade.handleMessagesWithSinkSource(Sink.ignore, testSource))
}
}
一切正常(我每秒从客户端收到 1 条测试消息)。唯一的问题是,如果客户端关闭 Web 套接字连接,我不明白如何停止/关闭 Source (testSource)。
如果网络套接字关闭,您也可以看到源继续生成元素(请参阅println)。
如何检测客户端断开连接?
【问题讨论】:
标签: scala websocket akka akka-stream akka-http