scala - 如何保存 websocket 客户端的连接并稍后使用 akka-streams 和 akka-http 发送
问题描述
我正在尝试遵循akka-http
文档的这一部分,其中讨论了异步处理 Web 套接字消息
我想做的是:
- 接收客户端的 websocket 请求
- 将付款发票返回给客户
- 运行一个后台进程,保存客户端的 websocket 连接,当客户端支付他们的发票时,发送他们查询的数据作为回报(在这种情况下是“世界”)。
这是我到目前为止的代码
def hello: Route = {
val amt = 1000
val helloRoute: Route = pathPrefix(Constants.apiVersion) {
path("hello") {
val source: Source[Message, SourceQueueWithComplete[Message]] = {
Source.queue(1, OverflowStrategy.backpressure)
}
val paymentRequest = createPaymentRequest(1000, extractUpgradeToWebSocket)
Directives.handleWebSocketMessages(
paymentFlow(paymentRequest)
)
}
}
helloRoute
}
private def createPaymentRequest(amt: Long, wsUpgrade: Directive1[UpgradeToWebSocket]) = {
val httpResponse: Directive1[HttpResponse] = wsUpgrade.map { ws =>
val sink: Sink[Message, NotUsed] = Sink.cancelled()
val source: Source[Message, NotUsed] = Source.single(TextMessage("World"))
val x: HttpResponse = ws.handleMessagesWithSinkSource(sink, source)
x
}
httpResponse.map { resp =>
//here is where I want to send a websocket message back to the client
//that is the HttpResponse above, how do I complete this?
Directives.complete(resp)
}
}
我似乎无法弄清楚如何访问容器类型的aRequestContext
或 a ?而当我在地图上的地图没有执行。UpgradeToWebSocket
Directive
httpResponse
解决方案
推荐阅读
- javascript - 为什么我的方法在点击时在 Framework7 中被调用两次?
- if-statement - 如何将替代值分配给相对于 Ox 中的另一个变量的变量(有点类似于 C++)
- jenkins - 包含所有参数值列表或参数列表中的单个值的 Jenkinsfile
- android - Google Play 游戏服务 - 如何切换到新的 Firebase 项目?
- javascript - 如何检查表 td 是否至少有一个值?
- botframework - 在 Bot Composer 中连接现有的 QnA 知识库
- matlab - 从matlab中的结构创建表 - 对齐
- c++ - 通常的算术转换如何工作?
- powerapps - 是否可以在桌面或浏览器的菜单栏中集成电源应用程序?
- docker - Windows 上的 Docker CLI 和 containerd