gpt4 book ai didi

scala - 播放框架 Scala : Create infinite source using scala akka streams and keep Server sent events connection open on server

转载 作者:行者123 更新时间:2023-12-04 17:44:51 24 4
gpt4 key购买 nike

我们需要为以下用例实现服务器发送的事件:

  1. 在服务器上进行一些处理后向用户界面发送通知。这个处理是基于一些逻辑的
  2. 在读取来自 RabbitMQ 的消息后向 UI 发送通知,然后对其执行一些操作。

我们使用 Scala(2.11/2.12) 和 Play 框架 (2.6.x) 的技术集。库:akka.stream.scaladsl.Source

我们从以下示例开始我们的概念验证 https://github.com/playframework/play-scala-streaming-example然后我们通过创建不同的源进行扩展。我们尝试使用 Source.apply,soure.single 创建源代码。

但是一旦源中的所有元素都被推送到 UI,我的事件流就关闭了。但我不希望事件流关闭。我也不想使用一些计时器(Source.tick)或 Source.repeat。

创建我的源时,集合假设有一些 x 元素,然后服务又添加了 4 个元素。但是在 x 个元素之后,事件流关闭然后再次重新打开。

有什么方法可以让我的事件流无限大,并且只有在我的 session 注销后才会关闭,或者我们可以明确地关闭它。

//KeepAlive 代码(如评论中所问)

   object NotficationUtil {

var userNotificationMap = Map[Integer, Queue[String]]()

def addUserNotification(userId: Integer, message: String) = {
var queue = userNotificationMap.getOrElse(userId, Queue[String]())
queue += message
userNotificationMap.put(userId, queue)

}

def pushNotification(userId: Integer): Source[JsValue, _] = {
var queue = userNotificationMap.getOrElse(userId, Queue[String]())
Source.single(Json.toJson(queue.dequeueAll { x => true }))
}
}
@Singleton
class EventSourceController @Inject() (cc: ControllerComponents) extends AbstractController(cc) with FlowFactory{

def pushNotifications(user_id:Integer) = Action {
val stream = NotficationUtil.pushNotification(user_id)
Ok.chunked(stream.keepAlive(50.second, ()=>Json.obj("data"->"heartbeat")) via EventSource.flow).as(ContentTypes.EVENT_STREAM)
}

}

最佳答案

使用下面的代码创建actorref和publisher

val (ref, sourcePublisher)= Source.actorRef[T](Int.MaxValue, OverflowStrategy.fail).toMat(Sink.asPublisher(true))(Keep.both).run()

并从该发布者创建您的来源

val testsource = Source
.fromPublisher[T](sourcePublisher)

并将您的监听器注册为

Ok.chunked(
testsource.keepAlive(
50.seconds,
() => Json.obj("data"->"heartbeat")) via EventSource.flow)
.as(ContentTypes.EVENT_STREAM)
.withHeaders("X-Accel-Buffering" -> "no", "Cache-Control" -> "no-cache")

将您的 json 数据发送给 ref actor,数据将作为事件流通过此源流向前端。希望对您有所帮助。

关于scala - 播放框架 Scala : Create infinite source using scala akka streams and keep Server sent events connection open on server,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52586223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com