SCALA Lift - Comet Actor 内的 S.param 访问

发布于 2024-12-12 08:02:24 字数 248 浏览 0 评论 0原文

我正在尝试检索 CometActor 中的 url 参数,以验证数据源是否与目的地匹配,例如,用户从房间 A 发送一条消息,该消息应该仅在房间 A 中接收和显示,而不是在房间 B 或 C 中。

我试过了:

S.param("message").openOr("") 

但是总是空的,这样可以吗?或者有其他方法可以阻止彗星消息到达不该到达的地方吗?

预先感谢您的任何帮助,非常感谢:)

I'm attempting to retrieve a url parameter within a CometActor to validate that the source of the data matches the destination, e.g A user sends a message from room A, which should be received and displayed only in room A, not B or C.

I've tried:

S.param("message").openOr("") 

But it's always empty, can this be done? Or is there another way to stop Comet messages going where they shouldn't?

Thanks in advance for any help, much appreciated :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

病毒体 2024-12-19 08:02:24

CometActor 存在于会话之外,因此无法访问(大部分)会话。解决方案是使用包含所需会话数据的初始化消息来初始化参与者。有某种帮助程序(也许在 LiftRules 中)可以做到这一点。我正在手机上凭记忆复述这一切,但希望这足以继续下去。

具体来说,您需要

for (
  session <- S.session
  message <- S.param("message")
) {
  session.setupComet("myCometActor", Some("unique name, if you want it"), message)
}

Boot.scala

执行以下操作:查看 LiftSession 再多一点。我认为可能有一种方法可以连接到 LiftRules 在会话创建时调用相关代码...

更新:如果我们发送一个包含以下内容的案例类,您的 CometActor 可能会是什么样子:

// ...
session.setupComet(
  "myCometActor",
  Some("unique name, if you want it"),
  Message(message)
)
// ...
case class Message(text: String)

class CometMessage extends CometActor { 
  override def lowPriority = {
    case Message(text) => {
      // do something here with the text, whether settings a SessionVar or even just a plain var
    }
  }
}

CometActors exist outside of the session and so don't have access to (most of) it. The solution is to initialize the actor with an initialization message containing the desired session data. There's some sort of helper, perhaps in LiftRules, to do that. I'm on my phone and recounting this from memory but hopefully it's enough to go on.

Specifically, you're going to want to do something like:

for (
  session <- S.session
  message <- S.param("message")
) {
  session.setupComet("myCometActor", Some("unique name, if you want it"), message)
}

in your Boot.scala.

Check out LiftSession for a little more. I think there might be a way to hook into LiftRules to have the relevant code called upon session creation...

Update: And here's what your CometActor might look like if we send a case class containing:

// ...
session.setupComet(
  "myCometActor",
  Some("unique name, if you want it"),
  Message(message)
)
// ...
case class Message(text: String)

class CometMessage extends CometActor { 
  override def lowPriority = {
    case Message(text) => {
      // do something here with the text, whether settings a SessionVar or even just a plain var
    }
  }
}
黒涩兲箜 2024-12-19 08:02:24

来自 Lift 的邮件列表

CometActor 存在于 HTTP 请求/响应周期之外。这意味着在 CometActor 中处理消息期间,您看不到 Req,因为在消息处理过程中永远没有可用的 Req。

与此线程上的一些帖子相反,S 上下文可以像 SessionVars 一样可用(但不能使用 RequestVars,因为 CometActor 超出了请求的范围)。

(...)

最好的办法是创建一个 SessionVar(或几个 SessionVar)来保存主机和端口。从片段中设置这些 SessionVar(如果尚未设置)。然后从您的 CometActor 访问 SessionVars。

这是一种可能的选择。但是,我个人的偏好是使用 NamedCometActorSnippet 将彗星插入页面,并立即向创建的彗星发送消息:

class MyCometSnippet extends NamedCometActorSnippet {

  val cometClass = "MyComet"
  val name = "MyComet-instance"

  for {
    liftSession <- S.session
    processingMode <- S.param("message")
  } yield {
    liftSession.sendCometActorMessage(cometClass, Full(name), SetMessageValue(message))
  }
}

From Lift's mailing list:

CometActors exist outside of the HTTP request/response cycle. This means that during the processing of a message in a CometActor, you don't get to see the Req because there's never a Req available as part of message processing.

Contrary to some of the posts on this thread, the S context is available as are SessionVars (but not RequestVars because the CometActor is outside of the scope of a request).

(...)

Your best bet is to create a SessionVar (or a few SessionVars) that hold the host and port. Set those SessionVars from a snippet (if they are not already set). Then access the SessionVars from your CometActors.

This is one possible option. However, my personal preference is to insert comets to page using NamedCometActorSnippet and send a message to created comet immediately:

class MyCometSnippet extends NamedCometActorSnippet {

  val cometClass = "MyComet"
  val name = "MyComet-instance"

  for {
    liftSession <- S.session
    processingMode <- S.param("message")
  } yield {
    liftSession.sendCometActorMessage(cometClass, Full(name), SetMessageValue(message))
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文